blob: ad0607faea9d66f8f1a90780ecfda271fb17fd58 [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 SetWindowLong(hwnd, GWL_USERDATA, 0);
97 }
98 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
99 if (!_this) {
100 vlog.info("null _this in %x, message %u", hwnd, msg);
101 return DefWindowProc(hwnd, msg, wParam, lParam);
102 }
103
104 try {
105 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
106 } catch (rdr::Exception& e) {
107 vlog.error("untrapped: %s", e.str());
108 }
109
110 return result;
111};
112
113RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
114 WNDCLASS wndClass;
115 wndClass.style = 0;
116 wndClass.lpfnWndProc = RfbPlayerProc;
117 wndClass.cbClsExtra = 0;
118 wndClass.cbWndExtra = 0;
119 wndClass.hInstance = instance = GetModuleHandle(0);
120 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000121 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000122 if (!wndClass.hIcon)
123 printf("unable to load icon:%ld", GetLastError());
124 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
125 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000126 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000127 wndClass.lpszClassName = _T("RfbPlayerClass");
128 classAtom = RegisterClass(&wndClass);
129 if (!classAtom) {
130 throw rdr::SystemException("unable to register RfbPlayer window class",
131 GetLastError());
132 }
133}
134
135RfbPlayerClass::~RfbPlayerClass() {
136 if (classAtom) {
137 UnregisterClass((const TCHAR*)classAtom, instance);
138 }
139}
140
141RfbPlayerClass baseClass;
142
143//
144// -=- RfbFrameClass
145
146//
147// Window class used to displaying the rfb data
148//
149
150class RfbFrameClass {
151public:
152 RfbFrameClass();
153 ~RfbFrameClass();
154 ATOM classAtom;
155 HINSTANCE instance;
156};
157
158LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
159 LRESULT result;
160
161 if (msg == WM_CREATE)
162 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
163 else if (msg == WM_DESTROY)
164 SetWindowLong(hwnd, GWL_USERDATA, 0);
165 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
166 if (!_this) {
167 vlog.info("null _this in %x, message %u", hwnd, msg);
168 return DefWindowProc(hwnd, msg, wParam, lParam);
169 }
170
171 try {
172 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
173 } catch (rdr::Exception& e) {
174 vlog.error("untrapped: %s", e.str());
175 }
176
177 return result;
178}
179
180RfbFrameClass::RfbFrameClass() : classAtom(0) {
181 WNDCLASS wndClass;
182 wndClass.style = 0;
183 wndClass.lpfnWndProc = FrameProc;
184 wndClass.cbClsExtra = 0;
185 wndClass.cbWndExtra = 0;
186 wndClass.hInstance = instance = GetModuleHandle(0);
187 wndClass.hIcon = 0;
188 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
189 wndClass.hbrBackground = 0;
190 wndClass.lpszMenuName = 0;
191 wndClass.lpszClassName = _T("RfbPlayerClass1");
192 classAtom = RegisterClass(&wndClass);
193 if (!classAtom) {
194 throw rdr::SystemException("unable to register RfbPlayer window class",
195 GetLastError());
196 }
197}
198
199RfbFrameClass::~RfbFrameClass() {
200 if (classAtom) {
201 UnregisterClass((const TCHAR*)classAtom, instance);
202 }
203}
204
205RfbFrameClass frameClass;
206
207//
208// -=- RfbPlayer instance implementation
209//
210
211RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000212 bool _autoplay = false, bool _acceptBell = false)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000213: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
george82e6883de2005-02-08 14:42:12 +0000214 autoplay(_autoplay), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000215 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000216 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000217 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000218 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000219
george82e6883de2005-02-08 14:42:12 +0000220 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000221
george823c8fbbf2005-01-24 11:09:08 +0000222 // Reset the full session time
223 strcpy(fullSessionTime, "00m:00s");
224
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000225 // Create the main window
226 const TCHAR* name = _T("RfbPlayer");
227 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000228 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000229 if (!mainHwnd) {
230 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
231 }
232 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
233
234 // Create the backing buffer
235 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000236 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000237
george8217e92cb2005-01-31 16:01:02 +0000238 // Open the session file
239 if (fileName) {
240 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000241 if (initTime > 0) setPos(initTime);
242 setSpeed(playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +0000243 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000244}
245
246RfbPlayer::~RfbPlayer() {
247 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000248 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000249 delete rfbReader->join();
250 rfbReader = 0;
251 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000252 if (mainHwnd) {
253 setVisible(false);
254 DestroyWindow(mainHwnd);
255 mainHwnd = 0;
256 }
george825beb62a2005-02-09 13:04:32 +0000257 if (buffer) delete buffer;
258 if (cutText) delete [] cutText;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000259 vlog.debug("~RfbPlayer done");
260}
261
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000262LRESULT
263RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
264 switch (msg) {
265
266 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000267
268 case WM_CREATE:
269 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000270 // Create the frame window
271 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
272 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
273 hwnd, 0, frameClass.instance, this);
274
george82d070c692005-01-19 16:44:04 +0000275 createToolBar(hwnd);
276
george82006f2792005-02-05 07:40:47 +0000277 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000278
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000279 return 0;
280 }
281
george827214b822004-12-12 07:02:51 +0000282 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000283
284 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000285 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000286 case ID_OPENFILE:
287 {
288 char curDir[_MAX_DIR];
289 static char filename[_MAX_PATH];
290 OPENFILENAME ofn;
291 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
292 GetCurrentDirectory(sizeof(curDir), curDir);
293
294 ofn.lStructSize = sizeof(OPENFILENAME);
295 ofn.hwndOwner = getMainHandle();
296 ofn.lpstrFile = filename;
297 ofn.nMaxFile = sizeof(filename);
298 ofn.lpstrInitialDir = curDir;
299 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
300 "All files (*.*)\0*.*\0";
301 ofn.lpstrDefExt = "rfb";
302 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
303 if (GetOpenFileName(&ofn))
304 openSessionFile(filename);
305 }
306 break;
george825c13c662005-01-27 14:48:23 +0000307 case ID_PLAY:
308 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000309 break;
310 case ID_PAUSE:
311 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000312 break;
313 case ID_STOP:
314 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000315 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000316 }
george825c13c662005-01-27 14:48:23 +0000317 break;
318 case ID_PLAYPAUSE:
319 if (isPaused()) {
320 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000321 } else {
322 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000323 }
george825c13c662005-01-27 14:48:23 +0000324 break;
george827549df42005-02-08 16:31:02 +0000325 case ID_GOTO:
326 {
327 GotoPosDialog gotoPosDlg;
328 if (gotoPosDlg.showDialog()) {
329 setPos(gotoPosDlg.getPos());
330 updatePos(getTimeOffset());
331 }
332 }
333 break;
george825c13c662005-01-27 14:48:23 +0000334 case ID_FULLSCREEN:
335 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
336 break;
george8231a36332005-02-06 17:27:34 +0000337 case ID_LOOP:
338 loopPlayback = !loopPlayback;
339 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
340 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
341 break;
george824ea27f62005-01-29 15:03:06 +0000342 case ID_RETURN:
343 // Update the speed if return pressed in speedEdit
344 if (speedEdit == GetFocus()) {
345 char speedStr[20], *stopStr;
346 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
347 double speed = strtod(speedStr, &stopStr);
348 if (speed > 0) {
349 speed = min(MAX_SPEED, speed);
350 // Update speedUpDown position
351 SendMessage(speedUpDown, UDM_SETPOS,
352 0, MAKELONG((short)(speed / 0.5), 0));
353 } else {
354 speed = getSpeed();
355 }
356 setSpeed(speed);
357 sprintf(speedStr, "%.2f", speed);
358 SetWindowText(speedEdit, speedStr);
359 }
360 break;
george8201aa6732005-02-06 17:13:03 +0000361 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000362 PostQuitMessage(0);
363 break;
george82ef5f7262005-02-08 15:09:26 +0000364 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000365 MessageBox(getMainHandle(),
366 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
367 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000368 }
369 break;
370
371 // Update frame's window size and add scrollbars if required
372
373 case WM_SIZE:
374 {
george82d070c692005-01-19 16:44:04 +0000375
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000376 Point old_offset = bufferToClient(Point(0, 0));
377
378 // Update the cached sizing information
379 RECT r;
380 GetClientRect(getMainHandle(), &r);
381 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
382 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
383
384 GetWindowRect(getFrameHandle(), &r);
385 window_size = Rect(r.left, r.top, r.right, r.bottom);
386 GetClientRect(getFrameHandle(), &r);
387 client_size = Rect(r.left, r.top, r.right, r.bottom);
388
389 // Determine whether scrollbars are required
390 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000391
392 // Resize the ToolBar
393 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000394
395 // Redraw if required
396 if (!old_offset.equals(bufferToClient(Point(0, 0))))
397 InvalidateRect(getFrameHandle(), 0, TRUE);
398 }
399 break;
george828a471482005-02-06 07:15:53 +0000400
401 // Process messages from posTrackBar
402
403 case WM_HSCROLL:
404 {
405 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
406 Pos *= sliderStepMs;
407
408 switch (LOWORD(wParam)) {
409 case TB_PAGEUP:
410 case TB_PAGEDOWN:
411 case TB_LINEUP:
412 case TB_LINEDOWN:
413 case TB_THUMBTRACK:
414 sliderDraging = true;
415 updatePos(Pos);
416 return 0;
417 case TB_ENDTRACK:
418 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000419 sliderDraging = false;
420 return 0;
421 default:
422 break;
423 }
424 }
425 break;
george829e6e6cc2005-01-29 13:12:05 +0000426
427 case WM_NOTIFY:
428 switch (((NMHDR*)lParam)->code) {
429 case UDN_DELTAPOS:
430 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000431 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000432 char speedStr[20] = "\0";
433 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
434 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
435 double speed;
436
george824ea27f62005-01-29 15:03:06 +0000437 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000438 if (upDown->iDelta > 0) {
439 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
440 } else {
george824ea27f62005-01-29 15:03:06 +0000441 // It's need to round the UpDown position
442 if ((upDown->iPos * 0.5) != getSpeed()) {
443 upDown->iDelta = 0;
444 lResult = TRUE;
445 }
george829e6e6cc2005-01-29 13:12:05 +0000446 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
447 }
448 _gcvt(speed, 5, speedStr);
449 sprintf(speedStr, "%.2f", speed);
450 SetWindowText(speedEdit, speedStr);
451 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000452 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000453 }
george824ea27f62005-01-29 15:03:06 +0000454 }
george829e6e6cc2005-01-29 13:12:05 +0000455 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000456
457 case WM_CLOSE:
458 vlog.debug("WM_CLOSE %x", getMainHandle());
459 PostQuitMessage(0);
460 break;
461 }
462
463 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
464}
465
466LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
467 switch (msg) {
468
469 case WM_PAINT:
470 {
george825beb62a2005-02-09 13:04:32 +0000471 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000472 seekMode = true;
473 return 0;
474 } else {
475 if (seekMode) {
476 seekMode = false;
477 InvalidateRect(getFrameHandle(), 0, true);
478 UpdateWindow(getFrameHandle());
479 return 0;
480 }
481 }
482
483 PAINTSTRUCT ps;
484 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
485 if (!paintDC)
486 throw SystemException("unable to BeginPaint", GetLastError());
487 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
488
489 if (!pr.is_empty()) {
490
491 if (buffer->bitmap) {
492
493 // Get device context
494 BitmapDC bitmapDC(paintDC, buffer->bitmap);
495
496 // Blit the border if required
497 Rect bufpos = bufferToClient(buffer->getRect());
498 if (!pr.enclosed_by(bufpos)) {
499 vlog.debug("draw border");
500 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
501 RECT r;
502 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
503 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
504 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
505 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
506 }
507
508 // Do the blit
509 Point buf_pos = clientToBuffer(pr.tl);
510 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
511 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
512 throw SystemException("unable to BitBlt to window", GetLastError());
513
514 } else {
515 // Blit a load of black
516 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
517 0, 0, 0, BLACKNESS))
518 throw SystemException("unable to BitBlt to blank window", GetLastError());
519 }
520 }
521 EndPaint(getFrameHandle(), &ps);
522 }
523 return 0;
524
525 case WM_VSCROLL:
526 case WM_HSCROLL:
527 {
528 Point delta;
529 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
530
531 switch (LOWORD(wParam)) {
532 case SB_PAGEUP: newpos -= 50; break;
533 case SB_PAGEDOWN: newpos += 50; break;
534 case SB_LINEUP: newpos -= 5; break;
535 case SB_LINEDOWN: newpos += 5; break;
536 case SB_THUMBTRACK:
537 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
538 default: vlog.info("received unknown scroll message");
539 };
540
541 if (msg == WM_HSCROLL)
542 setViewportOffset(Point(newpos, scrolloffset.y));
543 else
544 setViewportOffset(Point(scrolloffset.x, newpos));
545
546 SCROLLINFO si;
547 si.cbSize = sizeof(si);
548 si.fMask = SIF_POS;
549 si.nPos = newpos;
550 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
551 }
552 break;
553 }
554
555 return DefWindowProc(hwnd, msg, wParam, lParam);
556}
557
558void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000559 bool _autoplay = false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000560 autoplay = _autoplay;
561 playbackSpeed = _playbackSpeed;
562 initTime = _initTime;
563}
564
565void RfbPlayer::applyOptions() {
566 if (initTime >= 0)
567 setPos(initTime);
568 setSpeed(playbackSpeed);
569 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000570}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000571
george82d070c692005-01-19 16:44:04 +0000572void RfbPlayer::createToolBar(HWND parentHwnd) {
573 RECT tRect;
574 InitCommonControls();
575
576 tb.create(ID_TOOLBAR, parentHwnd);
577 tb.addBitmap(4, IDB_TOOLBAR);
578
579 // Create the control buttons
580 tb.addButton(0, ID_PLAY);
581 tb.addButton(1, ID_PAUSE);
582 tb.addButton(2, ID_STOP);
583 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
584 tb.addButton(3, ID_FULLSCREEN);
585 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
586
587 // Create the static control for the time output
588 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
589 tb.getButtonRect(6, &tRect);
590 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
591 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
592 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
593 GetModuleHandle(0), 0);
594 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
595
596 // Create the trackbar control for the time position
597 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
598 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000599 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000600 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
601 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
602 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
603 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000604 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000605 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
606
607 // Create the label with "Speed:" caption
608 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
609 tb.getButtonRect(10, &tRect);
610 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
611 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
612 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
613
614 // Create the edit control and the spin for the speed managing
615 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
616 tb.getButtonRect(11, &tRect);
617 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
618 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
619 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
620 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
621 // It's need to send notify messages to toolbar parent window
622 SetParent(speedEdit, tb.getHandle());
623
624 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
625 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000626 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000627}
628
629void RfbPlayer::setVisible(bool visible) {
630 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
631 if (visible) {
632 // When the window becomes visible, make it active
633 SetForegroundWindow(getMainHandle());
634 SetActiveWindow(getMainHandle());
635 }
636}
637
638void RfbPlayer::setTitle(const char *title) {
639 char _title[256];
640 strcpy(_title, AppName);
641 strcat(_title, " - ");
642 strcat(_title, title);
643 SetWindowText(getMainHandle(), _title);
644}
645
646void RfbPlayer::setFrameSize(int width, int height) {
647 // Calculate and set required size for main window
648 RECT r = {0, 0, width, height};
649 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
650 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
651 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
652 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
653 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
654 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
655
656 // Enable/disable scrollbars as appropriate
657 calculateScrollBars();
658}
659
660void RfbPlayer::calculateScrollBars() {
661 // Calculate the required size of window
662 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
663 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
664 DWORD old_style;
665 RECT r;
666 SetRect(&r, 0, 0, buffer->width(), buffer->height());
667 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
668 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
669
670 // Work out whether scroll bars are required
671 do {
672 old_style = style;
673
674 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
675 style |= WS_HSCROLL;
676 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
677 }
678 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
679 style |= WS_VSCROLL;
680 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
681 }
682 } while (style != old_style);
683
684 // Tell Windows to update the window style & cached settings
685 if (style != current_style) {
686 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
687 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
688 }
689
690 // Update the scroll settings
691 SCROLLINFO si;
692 if (style & WS_VSCROLL) {
693 si.cbSize = sizeof(si);
694 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
695 si.nMin = 0;
696 si.nMax = buffer->height();
697 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
698 maxscrolloffset.y = max(0, si.nMax-si.nPage);
699 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
700 si.nPos = scrolloffset.y;
701 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
702 }
703 if (style & WS_HSCROLL) {
704 si.cbSize = sizeof(si);
705 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
706 si.nMin = 0;
707 si.nMax = buffer->width();
708 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
709 maxscrolloffset.x = max(0, si.nMax-si.nPage);
710 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
711 si.nPos = scrolloffset.x;
712 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
713 }
714}
715
716bool RfbPlayer::setViewportOffset(const Point& tl) {
717/* ***
718 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
719 max(0, min(maxscrolloffset.y, tl.y)));
720 */
721 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
722 max(0, min(tl.y, buffer->height()-client_size.height())));
723 Point delta = np.translate(scrolloffset.negate());
724 if (!np.equals(scrolloffset)) {
725 scrolloffset = np;
726 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
727 UpdateWindow(getFrameHandle());
728 return true;
729 }
730 return false;
731}
732
733void RfbPlayer::close(const char* reason) {
734 setVisible(false);
735 if (reason) {
736 vlog.info("closing - %s", reason);
737 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
738 }
739 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
740}
741
742void RfbPlayer::blankBuffer() {
743 fillRect(buffer->getRect(), 0);
744}
745
746void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000747 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000748 blankBuffer();
749 newSession(fileName);
750 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000751 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000752 if (paused) is->pausePlayback();
753 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000754}
755
756void RfbPlayer::processMsg() {
757 static long update_time = GetTickCount();
758 try {
george828a471482005-02-06 07:15:53 +0000759 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
760 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000761 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000762 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000763 update_time = GetTickCount();
764 }
765 RfbProto::processMsg();
766 } catch (rdr::Exception e) {
767 if (strcmp(e.str(), "[End Of File]") == 0) {
768 rewind();
george8231a36332005-02-06 17:27:34 +0000769 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000770 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000771 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000772 return;
773 }
774 // It's a special exception to perform backward seeking.
775 // We only rewind the stream and seek the offset
776 if (strcmp(e.str(), "[REWIND]") == 0) {
777 long initTime = getSeekOffset();
778 rewind();
779 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000780 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000781 } else {
782 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
783 return;
784 }
785 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000786}
787
788void RfbPlayer::serverInit() {
789 RfbProto::serverInit();
790
791 // Save the server init time for using in setPos()
792 serverInitTime = getTimeOffset() / getSpeed();
793
794 // Resize the backing buffer
795 buffer->setSize(cp.width, cp.height);
796
797 // Check on the true colour mode
798 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000799 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000800
801 // Set the session pixel format
802 buffer->setPF(cp.pf());
803
804 // If the window is not maximised then resize it
805 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
806 setFrameSize(cp.width, cp.height);
807
808 // Set the window title and show it
809 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000810
george82d4d69e62005-02-05 09:23:18 +0000811 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000812 sessionTimeMs = calculateSessionTime(fileName);
813 sprintf(fullSessionTime, "%.2um:%.2us",
814 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000815 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000816 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
817 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000818 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000819
george82006f2792005-02-05 07:40:47 +0000820 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000821}
822
823void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
824 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
825 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
826/* int i;
827 for (i=0;i<count;i++) {
828 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
829 }
830 // *** change to 0, 256?
831 refreshWindowPalette(first, count);
832 palette_changed = true;
833 InvalidateRect(getFrameHandle(), 0, FALSE);*/
834}
835
836void RfbPlayer::bell() {
837 if (acceptBell)
838 MessageBeep(-1);
839}
840
841void RfbPlayer::serverCutText(const char* str, int len) {
842 if (cutText != NULL)
843 delete [] cutText;
844 cutText = new char[len + 1];
845 memcpy(cutText, str, len);
846 cutText[len] = '\0';
847}
848
849void RfbPlayer::frameBufferUpdateEnd() {
850};
851
852void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
853}
854
855void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
856}
857
858
859void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
860 buffer->fillRect(r, pix);
861 invalidateBufferRect(r);
862}
863
864void RfbPlayer::imageRect(const Rect& r, void* pixels) {
865 buffer->imageRect(r, pixels);
866 invalidateBufferRect(r);
867}
868
869void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
870 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
871 invalidateBufferRect(r);
872}
873
874bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
875 Rect rect = bufferToClient(crect);
876 if (rect.intersect(client_size).is_empty()) return false;
877 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
878 InvalidateRect(getFrameHandle(), &invalid, FALSE);
879 return true;
880}
881
george8257f13522005-02-05 08:48:22 +0000882long RfbPlayer::calculateSessionTime(char *filename) {
883 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000884 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000885 try {
886 while (TRUE) {
887 sessionFile.skip(1024);
888 }
889 } catch (rdr::Exception e) {
890 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000891 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000892 } else {
893 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
894 return 0;
895 }
896 }
897 return 0;
898}
899
george8217e92cb2005-01-31 16:01:02 +0000900void RfbPlayer::openSessionFile(char *_fileName) {
901 fileName = strDup(_fileName);
902
903 // Close the previous reading thread
904 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000905 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000906 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000907 }
908 blankBuffer();
909 newSession(fileName);
910 setSpeed(playbackSpeed);
911 rfbReader = new rfbSessionReader(this);
912 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000913 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8217e92cb2005-01-31 16:01:02 +0000914}
915
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000916void RfbPlayer::setPaused(bool paused) {
917 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000918 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000919 tb.checkButton(ID_PAUSE, true);
920 tb.checkButton(ID_PLAY, false);
921 tb.checkButton(ID_STOP, false);
922 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
923 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000924 } else {
george825beb62a2005-02-09 13:04:32 +0000925 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000926 tb.checkButton(ID_PLAY, true);
927 tb.checkButton(ID_STOP, false);
928 tb.checkButton(ID_PAUSE, false);
929 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
930 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000931 }
932}
933
george82006f2792005-02-05 07:40:47 +0000934void RfbPlayer::stopPlayback() {
935 setPos(0);
george825beb62a2005-02-09 13:04:32 +0000936 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000937 tb.checkButton(ID_STOP, true);
938 tb.checkButton(ID_PLAY, false);
939 tb.checkButton(ID_PAUSE, false);
940 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
941 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000942 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000943}
944
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000945void RfbPlayer::setSpeed(double speed) {
946 serverInitTime = serverInitTime * getSpeed() / speed;
947 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000948 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000949}
950
951double RfbPlayer::getSpeed() {
952 return is->getSpeed();
953}
954
955void RfbPlayer::setPos(long pos) {
956 is->setTimeOffset(max(pos, serverInitTime));
957}
958
959long RfbPlayer::getSeekOffset() {
960 return is->getSeekOffset();
961}
962
963bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +0000964 if (is) return is->isSeeking();
965 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000966}
967
968bool RfbPlayer::isSeekMode() {
969 return seekMode;
970}
971
972bool RfbPlayer::isPaused() {
973 return is->isPaused();
974}
975
976long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +0000977 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000978}
979
george828a471482005-02-06 07:15:53 +0000980void RfbPlayer::updatePos(long newPos) {
981 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +0000982 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +0000983 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +0000984 newPos /= 1000;
george8244325492005-02-06 07:29:51 +0000985 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +0000986 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +0000987
988 // Update the position of slider
989 if (!sliderDraging) {
990 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +0000991 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
992 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +0000993 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000994}
995
996void RfbPlayer::skipHandshaking() {
997 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
998 is->skip(skipBytes);
999 state_ = RFBSTATE_NORMAL;
1000}
1001
1002void programInfo() {
1003 win32::FileVersionInfo inf;
1004 _tprintf(_T("%s - %s, Version %s\n"),
1005 inf.getVerString(_T("ProductName")),
1006 inf.getVerString(_T("FileDescription")),
1007 inf.getVerString(_T("FileVersion")));
1008 printf("%s\n", buildTime);
1009 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1010}
1011
1012void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001013 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001014}
1015
1016double playbackSpeed = 1.0;
1017long initTime = -1;
1018bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001019char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001020bool print_usage = false;
1021bool acceptBell = false;
1022
1023bool processParams(int argc, char* argv[]) {
1024 for (int i = 1; i < argc; i++) {
1025 if ((strcasecmp(argv[i], "-help") == 0) ||
1026 (strcasecmp(argv[i], "--help") == 0) ||
1027 (strcasecmp(argv[i], "/help") == 0) ||
1028 (strcasecmp(argv[i], "-h") == 0) ||
1029 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001030 (strcasecmp(argv[i], "/?") == 0) ||
1031 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001032 print_usage = true;
1033 return true;
1034 }
1035
1036 if ((strcasecmp(argv[i], "-speed") == 0) ||
1037 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1038 playbackSpeed = atof(argv[++i]);
1039 if (playbackSpeed <= 0) {
1040 return false;
1041 }
1042 continue;
1043 }
1044
1045 if ((strcasecmp(argv[i], "-pos") == 0) ||
1046 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1047 initTime = atol(argv[++i]);
1048 if (initTime <= 0)
1049 return false;
1050 continue;
1051 }
1052
1053 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1054 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001055 autoplay = true;
1056 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001057 }
1058
1059 if ((strcasecmp(argv[i], "-bell") == 0) ||
1060 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001061 acceptBell = true;
1062 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001063 }
1064
1065 if (i != argc - 1)
1066 return false;
1067 }
1068
1069 fileName = strDup(argv[argc-1]);
1070 return true;
1071}
1072
1073//
1074// -=- WinMain
1075//
1076
1077int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1078
1079 // - Process the command-line
1080
1081 int argc = __argc;
1082 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001083 if ((argc > 1) && (!processParams(argc, argv))) {
1084 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1085 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001086 }
george82e6883de2005-02-08 14:42:12 +00001087
1088 if (print_usage) {
1089 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001090 return 0;
george8267cbcd02005-01-16 15:39:56 +00001091 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001092
george82e6883de2005-02-08 14:42:12 +00001093 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001094 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001095 try {
1096 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001097 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001098 } catch (rdr::Exception e) {
1099 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1100 delete player;
1101 return 0;
1102 }
1103
1104 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001105 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001106 MSG msg;
1107 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001108 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1109 TranslateMessage(&msg);
1110 DispatchMessage(&msg);
1111 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001112 }
1113
george82e6883de2005-02-08 14:42:12 +00001114 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001115 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001116 if (player) delete player;
1117 } catch (rdr::Exception e) {
1118 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1119 }
1120
1121 return 0;
1122};