blob: ab446cc382d56a2fec37233f7a9d2c805cae224d [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
21#include <conio.h>
22
23#include <rfb/LogWriter.h>
24#include <rfb/Exception.h>
25#include <rfb/Threading.h>
26
27#include <rfb_win32/Win32Util.h>
28#include <rfb_win32/WMShatter.h>
29
30#include <rfbplayer/rfbplayer.h>
31#include <rfbplayer/utils.h>
32#include <rfbplayer/resource.h>
33
34using namespace rfb;
35using namespace rfb::win32;
36
37// -=- Variables & consts
38
39static LogWriter vlog("RfbPlayer");
40
41TStr rfb::win32::AppName("RfbPlayer");
42extern const char* buildTime;
43
44// -=- RfbPlayer's defines
45
46#define strcasecmp _stricmp
george824ea27f62005-01-29 15:03:06 +000047#define MAX_SPEED 10
george82d4d69e62005-02-05 09:23:18 +000048#define MAX_POS_TRACKBAR_RANGE 50
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000049
george82d070c692005-01-19 16:44:04 +000050#define ID_TOOLBAR 500
51#define ID_PLAY 510
52#define ID_PAUSE 520
53#define ID_TIME_STATIC 530
54#define ID_SPEED_STATIC 540
55#define ID_SPEED_EDIT 550
56#define ID_POS_TRACKBAR 560
57#define ID_SPEED_UPDOWN 570
58
59
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000060//
61// -=- RfbPlayerClass
62
63//
64// Window class used as the basis for RfbPlayer instance
65//
66
67class RfbPlayerClass {
68public:
69 RfbPlayerClass();
70 ~RfbPlayerClass();
71 ATOM classAtom;
72 HINSTANCE instance;
73};
74
75LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
76 LRESULT result;
77
78 if (msg == WM_CREATE)
79 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
80 else if (msg == WM_DESTROY) {
81 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000082
83 // Resume playback (It's need to quit from FbsInputStream::waitWhilePaused())
84 _this->setPaused(false);
85 SetWindowLong(hwnd, GWL_USERDATA, 0);
86 }
87 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
88 if (!_this) {
89 vlog.info("null _this in %x, message %u", hwnd, msg);
90 return DefWindowProc(hwnd, msg, wParam, lParam);
91 }
92
93 try {
94 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
95 } catch (rdr::Exception& e) {
96 vlog.error("untrapped: %s", e.str());
97 }
98
99 return result;
100};
101
102RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
103 WNDCLASS wndClass;
104 wndClass.style = 0;
105 wndClass.lpfnWndProc = RfbPlayerProc;
106 wndClass.cbClsExtra = 0;
107 wndClass.cbWndExtra = 0;
108 wndClass.hInstance = instance = GetModuleHandle(0);
109 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000110 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000111 if (!wndClass.hIcon)
112 printf("unable to load icon:%ld", GetLastError());
113 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
114 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000115 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000116 wndClass.lpszClassName = _T("RfbPlayerClass");
117 classAtom = RegisterClass(&wndClass);
118 if (!classAtom) {
119 throw rdr::SystemException("unable to register RfbPlayer window class",
120 GetLastError());
121 }
122}
123
124RfbPlayerClass::~RfbPlayerClass() {
125 if (classAtom) {
126 UnregisterClass((const TCHAR*)classAtom, instance);
127 }
128}
129
130RfbPlayerClass baseClass;
131
132//
133// -=- RfbFrameClass
134
135//
136// Window class used to displaying the rfb data
137//
138
139class RfbFrameClass {
140public:
141 RfbFrameClass();
142 ~RfbFrameClass();
143 ATOM classAtom;
144 HINSTANCE instance;
145};
146
147LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
148 LRESULT result;
149
150 if (msg == WM_CREATE)
151 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
152 else if (msg == WM_DESTROY)
153 SetWindowLong(hwnd, GWL_USERDATA, 0);
154 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
155 if (!_this) {
156 vlog.info("null _this in %x, message %u", hwnd, msg);
157 return DefWindowProc(hwnd, msg, wParam, lParam);
158 }
159
160 try {
161 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
162 } catch (rdr::Exception& e) {
163 vlog.error("untrapped: %s", e.str());
164 }
165
166 return result;
167}
168
169RfbFrameClass::RfbFrameClass() : classAtom(0) {
170 WNDCLASS wndClass;
171 wndClass.style = 0;
172 wndClass.lpfnWndProc = FrameProc;
173 wndClass.cbClsExtra = 0;
174 wndClass.cbWndExtra = 0;
175 wndClass.hInstance = instance = GetModuleHandle(0);
176 wndClass.hIcon = 0;
177 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
178 wndClass.hbrBackground = 0;
179 wndClass.lpszMenuName = 0;
180 wndClass.lpszClassName = _T("RfbPlayerClass1");
181 classAtom = RegisterClass(&wndClass);
182 if (!classAtom) {
183 throw rdr::SystemException("unable to register RfbPlayer window class",
184 GetLastError());
185 }
186}
187
188RfbFrameClass::~RfbFrameClass() {
189 if (classAtom) {
190 UnregisterClass((const TCHAR*)classAtom, instance);
191 }
192}
193
194RfbFrameClass frameClass;
195
196//
197// -=- RfbPlayer instance implementation
198//
199
200RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
201 bool _autoplay = false, bool _showControls = true,
202 bool _acceptBell = false)
203: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
204 autoplay(_autoplay), showControls(_showControls), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000205 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000206 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000207 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
208 sliderDraging(false), sliderStepMs(0) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000209
210 if (showControls)
george82d070c692005-01-19 16:44:04 +0000211 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000212 else
213 CTRL_BAR_HEIGHT = 0;
214
george823c8fbbf2005-01-24 11:09:08 +0000215 // Reset the full session time
216 strcpy(fullSessionTime, "00m:00s");
217
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000218 // Create the main window
219 const TCHAR* name = _T("RfbPlayer");
220 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000221 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000222 if (!mainHwnd) {
223 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
224 }
225 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
226
227 // Create the backing buffer
228 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000229 setVisible(true);
george8217e92cb2005-01-31 16:01:02 +0000230
231 // Open the session file
232 if (fileName) {
233 openSessionFile(fileName);
234 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000235}
236
237RfbPlayer::~RfbPlayer() {
238 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000239 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000240 delete rfbReader->join();
241 rfbReader = 0;
242 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000243 if (mainHwnd) {
244 setVisible(false);
245 DestroyWindow(mainHwnd);
246 mainHwnd = 0;
247 }
248 delete buffer;
249 delete cutText;
250 vlog.debug("~RfbPlayer done");
251}
252
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000253LRESULT
254RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
255 switch (msg) {
256
257 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000258
259 case WM_CREATE:
260 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000261 // Create the frame window
262 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
263 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
264 hwnd, 0, frameClass.instance, this);
265
george82d070c692005-01-19 16:44:04 +0000266 createToolBar(hwnd);
267
george82006f2792005-02-05 07:40:47 +0000268 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000269
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000270 return 0;
271 }
272
george827214b822004-12-12 07:02:51 +0000273 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000274
275 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000276 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000277 case ID_OPENFILE:
278 {
279 char curDir[_MAX_DIR];
280 static char filename[_MAX_PATH];
281 OPENFILENAME ofn;
282 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
283 GetCurrentDirectory(sizeof(curDir), curDir);
284
285 ofn.lStructSize = sizeof(OPENFILENAME);
286 ofn.hwndOwner = getMainHandle();
287 ofn.lpstrFile = filename;
288 ofn.nMaxFile = sizeof(filename);
289 ofn.lpstrInitialDir = curDir;
290 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
291 "All files (*.*)\0*.*\0";
292 ofn.lpstrDefExt = "rfb";
293 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
294 if (GetOpenFileName(&ofn))
295 openSessionFile(filename);
296 }
297 break;
george825c13c662005-01-27 14:48:23 +0000298 case ID_PLAY:
299 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000300 break;
301 case ID_PAUSE:
302 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000303 break;
304 case ID_STOP:
305 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000306 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000307 }
george825c13c662005-01-27 14:48:23 +0000308 break;
309 case ID_PLAYPAUSE:
310 if (isPaused()) {
311 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000312 } else {
313 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000314 }
george825c13c662005-01-27 14:48:23 +0000315 break;
316 case ID_FULLSCREEN:
317 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
318 break;
george824ea27f62005-01-29 15:03:06 +0000319 case ID_RETURN:
320 // Update the speed if return pressed in speedEdit
321 if (speedEdit == GetFocus()) {
322 char speedStr[20], *stopStr;
323 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
324 double speed = strtod(speedStr, &stopStr);
325 if (speed > 0) {
326 speed = min(MAX_SPEED, speed);
327 // Update speedUpDown position
328 SendMessage(speedUpDown, UDM_SETPOS,
329 0, MAKELONG((short)(speed / 0.5), 0));
330 } else {
331 speed = getSpeed();
332 }
333 setSpeed(speed);
334 sprintf(speedStr, "%.2f", speed);
335 SetWindowText(speedEdit, speedStr);
336 }
337 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000338 }
339 break;
340
341 // Update frame's window size and add scrollbars if required
342
343 case WM_SIZE:
344 {
george82d070c692005-01-19 16:44:04 +0000345
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000346 Point old_offset = bufferToClient(Point(0, 0));
347
348 // Update the cached sizing information
349 RECT r;
350 GetClientRect(getMainHandle(), &r);
351 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
352 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
353
354 GetWindowRect(getFrameHandle(), &r);
355 window_size = Rect(r.left, r.top, r.right, r.bottom);
356 GetClientRect(getFrameHandle(), &r);
357 client_size = Rect(r.left, r.top, r.right, r.bottom);
358
359 // Determine whether scrollbars are required
360 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000361
362 // Resize the ToolBar
363 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000364
365 // Redraw if required
366 if (!old_offset.equals(bufferToClient(Point(0, 0))))
367 InvalidateRect(getFrameHandle(), 0, TRUE);
368 }
369 break;
george828a471482005-02-06 07:15:53 +0000370
371 // Process messages from posTrackBar
372
373 case WM_HSCROLL:
374 {
375 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
376 Pos *= sliderStepMs;
377
378 switch (LOWORD(wParam)) {
379 case TB_PAGEUP:
380 case TB_PAGEDOWN:
381 case TB_LINEUP:
382 case TB_LINEDOWN:
383 case TB_THUMBTRACK:
384 sliderDraging = true;
385 updatePos(Pos);
386 return 0;
387 case TB_ENDTRACK:
388 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000389 sliderDraging = false;
390 return 0;
391 default:
392 break;
393 }
394 }
395 break;
george829e6e6cc2005-01-29 13:12:05 +0000396
397 case WM_NOTIFY:
398 switch (((NMHDR*)lParam)->code) {
399 case UDN_DELTAPOS:
400 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000401 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000402 char speedStr[20] = "\0";
403 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
404 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
405 double speed;
406
george824ea27f62005-01-29 15:03:06 +0000407 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000408 if (upDown->iDelta > 0) {
409 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
410 } else {
george824ea27f62005-01-29 15:03:06 +0000411 // It's need to round the UpDown position
412 if ((upDown->iPos * 0.5) != getSpeed()) {
413 upDown->iDelta = 0;
414 lResult = TRUE;
415 }
george829e6e6cc2005-01-29 13:12:05 +0000416 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
417 }
418 _gcvt(speed, 5, speedStr);
419 sprintf(speedStr, "%.2f", speed);
420 SetWindowText(speedEdit, speedStr);
421 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000422 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000423 }
george824ea27f62005-01-29 15:03:06 +0000424 }
george829e6e6cc2005-01-29 13:12:05 +0000425 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000426
427 case WM_CLOSE:
428 vlog.debug("WM_CLOSE %x", getMainHandle());
429 PostQuitMessage(0);
430 break;
431 }
432
433 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
434}
435
436LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
437 switch (msg) {
438
439 case WM_PAINT:
440 {
441 if (is->isSeeking()) {
442 seekMode = true;
443 return 0;
444 } else {
445 if (seekMode) {
446 seekMode = false;
447 InvalidateRect(getFrameHandle(), 0, true);
448 UpdateWindow(getFrameHandle());
449 return 0;
450 }
451 }
452
453 PAINTSTRUCT ps;
454 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
455 if (!paintDC)
456 throw SystemException("unable to BeginPaint", GetLastError());
457 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
458
459 if (!pr.is_empty()) {
460
461 if (buffer->bitmap) {
462
463 // Get device context
464 BitmapDC bitmapDC(paintDC, buffer->bitmap);
465
466 // Blit the border if required
467 Rect bufpos = bufferToClient(buffer->getRect());
468 if (!pr.enclosed_by(bufpos)) {
469 vlog.debug("draw border");
470 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
471 RECT r;
472 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
473 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
474 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
475 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
476 }
477
478 // Do the blit
479 Point buf_pos = clientToBuffer(pr.tl);
480 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
481 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
482 throw SystemException("unable to BitBlt to window", GetLastError());
483
484 } else {
485 // Blit a load of black
486 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
487 0, 0, 0, BLACKNESS))
488 throw SystemException("unable to BitBlt to blank window", GetLastError());
489 }
490 }
491 EndPaint(getFrameHandle(), &ps);
492 }
493 return 0;
494
495 case WM_VSCROLL:
496 case WM_HSCROLL:
497 {
498 Point delta;
499 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
500
501 switch (LOWORD(wParam)) {
502 case SB_PAGEUP: newpos -= 50; break;
503 case SB_PAGEDOWN: newpos += 50; break;
504 case SB_LINEUP: newpos -= 5; break;
505 case SB_LINEDOWN: newpos += 5; break;
506 case SB_THUMBTRACK:
507 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
508 default: vlog.info("received unknown scroll message");
509 };
510
511 if (msg == WM_HSCROLL)
512 setViewportOffset(Point(newpos, scrolloffset.y));
513 else
514 setViewportOffset(Point(scrolloffset.x, newpos));
515
516 SCROLLINFO si;
517 si.cbSize = sizeof(si);
518 si.fMask = SIF_POS;
519 si.nPos = newpos;
520 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
521 }
522 break;
523 }
524
525 return DefWindowProc(hwnd, msg, wParam, lParam);
526}
527
528void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
529 bool _autoplay = false, bool _showControls = true) {
530 showControls = _showControls;
531 autoplay = _autoplay;
532 playbackSpeed = _playbackSpeed;
533 initTime = _initTime;
534}
535
536void RfbPlayer::applyOptions() {
537 if (initTime >= 0)
538 setPos(initTime);
539 setSpeed(playbackSpeed);
540 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000541}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000542
george82d070c692005-01-19 16:44:04 +0000543void RfbPlayer::createToolBar(HWND parentHwnd) {
544 RECT tRect;
545 InitCommonControls();
546
547 tb.create(ID_TOOLBAR, parentHwnd);
548 tb.addBitmap(4, IDB_TOOLBAR);
549
550 // Create the control buttons
551 tb.addButton(0, ID_PLAY);
552 tb.addButton(1, ID_PAUSE);
553 tb.addButton(2, ID_STOP);
554 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
555 tb.addButton(3, ID_FULLSCREEN);
556 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
557
558 // Create the static control for the time output
559 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
560 tb.getButtonRect(6, &tRect);
561 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
562 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
563 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
564 GetModuleHandle(0), 0);
565 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
566
567 // Create the trackbar control for the time position
568 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
569 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000570 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000571 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
572 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
573 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
574 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000575 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000576 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
577
578 // Create the label with "Speed:" caption
579 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
580 tb.getButtonRect(10, &tRect);
581 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
582 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
583 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
584
585 // Create the edit control and the spin for the speed managing
586 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
587 tb.getButtonRect(11, &tRect);
588 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
589 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
590 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
591 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
592 // It's need to send notify messages to toolbar parent window
593 SetParent(speedEdit, tb.getHandle());
594
595 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
596 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000597 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000598}
599
600void RfbPlayer::setVisible(bool visible) {
601 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
602 if (visible) {
603 // When the window becomes visible, make it active
604 SetForegroundWindow(getMainHandle());
605 SetActiveWindow(getMainHandle());
606 }
607}
608
609void RfbPlayer::setTitle(const char *title) {
610 char _title[256];
611 strcpy(_title, AppName);
612 strcat(_title, " - ");
613 strcat(_title, title);
614 SetWindowText(getMainHandle(), _title);
615}
616
617void RfbPlayer::setFrameSize(int width, int height) {
618 // Calculate and set required size for main window
619 RECT r = {0, 0, width, height};
620 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
621 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
622 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
623 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
624 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
625 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
626
627 // Enable/disable scrollbars as appropriate
628 calculateScrollBars();
629}
630
631void RfbPlayer::calculateScrollBars() {
632 // Calculate the required size of window
633 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
634 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
635 DWORD old_style;
636 RECT r;
637 SetRect(&r, 0, 0, buffer->width(), buffer->height());
638 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
639 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
640
641 // Work out whether scroll bars are required
642 do {
643 old_style = style;
644
645 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
646 style |= WS_HSCROLL;
647 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
648 }
649 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
650 style |= WS_VSCROLL;
651 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
652 }
653 } while (style != old_style);
654
655 // Tell Windows to update the window style & cached settings
656 if (style != current_style) {
657 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
658 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
659 }
660
661 // Update the scroll settings
662 SCROLLINFO si;
663 if (style & WS_VSCROLL) {
664 si.cbSize = sizeof(si);
665 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
666 si.nMin = 0;
667 si.nMax = buffer->height();
668 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
669 maxscrolloffset.y = max(0, si.nMax-si.nPage);
670 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
671 si.nPos = scrolloffset.y;
672 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
673 }
674 if (style & WS_HSCROLL) {
675 si.cbSize = sizeof(si);
676 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
677 si.nMin = 0;
678 si.nMax = buffer->width();
679 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
680 maxscrolloffset.x = max(0, si.nMax-si.nPage);
681 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
682 si.nPos = scrolloffset.x;
683 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
684 }
685}
686
687bool RfbPlayer::setViewportOffset(const Point& tl) {
688/* ***
689 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
690 max(0, min(maxscrolloffset.y, tl.y)));
691 */
692 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
693 max(0, min(tl.y, buffer->height()-client_size.height())));
694 Point delta = np.translate(scrolloffset.negate());
695 if (!np.equals(scrolloffset)) {
696 scrolloffset = np;
697 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
698 UpdateWindow(getFrameHandle());
699 return true;
700 }
701 return false;
702}
703
704void RfbPlayer::close(const char* reason) {
705 setVisible(false);
706 if (reason) {
707 vlog.info("closing - %s", reason);
708 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
709 }
710 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
711}
712
713void RfbPlayer::blankBuffer() {
714 fillRect(buffer->getRect(), 0);
715}
716
717void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000718 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000719 blankBuffer();
720 newSession(fileName);
721 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000722 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000723 if (paused) is->pausePlayback();
724 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000725}
726
727void RfbPlayer::processMsg() {
728 static long update_time = GetTickCount();
729 try {
george828a471482005-02-06 07:15:53 +0000730 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
731 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000732 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000733 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000734 update_time = GetTickCount();
735 }
736 RfbProto::processMsg();
737 } catch (rdr::Exception e) {
738 if (strcmp(e.str(), "[End Of File]") == 0) {
739 rewind();
740 setPaused(true);
george828a471482005-02-06 07:15:53 +0000741 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000742 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000743 return;
744 }
745 // It's a special exception to perform backward seeking.
746 // We only rewind the stream and seek the offset
747 if (strcmp(e.str(), "[REWIND]") == 0) {
748 long initTime = getSeekOffset();
749 rewind();
750 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000751 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000752 } else {
753 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
754 return;
755 }
756 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000757}
758
759void RfbPlayer::serverInit() {
760 RfbProto::serverInit();
761
762 // Save the server init time for using in setPos()
763 serverInitTime = getTimeOffset() / getSpeed();
764
765 // Resize the backing buffer
766 buffer->setSize(cp.width, cp.height);
767
768 // Check on the true colour mode
769 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000770 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000771
772 // Set the session pixel format
773 buffer->setPF(cp.pf());
774
775 // If the window is not maximised then resize it
776 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
777 setFrameSize(cp.width, cp.height);
778
779 // Set the window title and show it
780 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000781
george82d4d69e62005-02-05 09:23:18 +0000782 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000783 sessionTimeMs = calculateSessionTime(fileName);
784 sprintf(fullSessionTime, "%.2um:%.2us",
785 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000786 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000787 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
788 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000789 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000790
george82006f2792005-02-05 07:40:47 +0000791 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000792}
793
794void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
795 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
796 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
797/* int i;
798 for (i=0;i<count;i++) {
799 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
800 }
801 // *** change to 0, 256?
802 refreshWindowPalette(first, count);
803 palette_changed = true;
804 InvalidateRect(getFrameHandle(), 0, FALSE);*/
805}
806
807void RfbPlayer::bell() {
808 if (acceptBell)
809 MessageBeep(-1);
810}
811
812void RfbPlayer::serverCutText(const char* str, int len) {
813 if (cutText != NULL)
814 delete [] cutText;
815 cutText = new char[len + 1];
816 memcpy(cutText, str, len);
817 cutText[len] = '\0';
818}
819
820void RfbPlayer::frameBufferUpdateEnd() {
821};
822
823void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
824}
825
826void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
827}
828
829
830void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
831 buffer->fillRect(r, pix);
832 invalidateBufferRect(r);
833}
834
835void RfbPlayer::imageRect(const Rect& r, void* pixels) {
836 buffer->imageRect(r, pixels);
837 invalidateBufferRect(r);
838}
839
840void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
841 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
842 invalidateBufferRect(r);
843}
844
845bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
846 Rect rect = bufferToClient(crect);
847 if (rect.intersect(client_size).is_empty()) return false;
848 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
849 InvalidateRect(getFrameHandle(), &invalid, FALSE);
850 return true;
851}
852
george8257f13522005-02-05 08:48:22 +0000853long RfbPlayer::calculateSessionTime(char *filename) {
854 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000855 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000856 try {
857 while (TRUE) {
858 sessionFile.skip(1024);
859 }
860 } catch (rdr::Exception e) {
861 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000862 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000863 } else {
864 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
865 return 0;
866 }
867 }
868 return 0;
869}
870
george8217e92cb2005-01-31 16:01:02 +0000871void RfbPlayer::openSessionFile(char *_fileName) {
872 fileName = strDup(_fileName);
873
874 // Close the previous reading thread
875 if (rfbReader) {
george826e51fcc2005-02-06 13:30:49 +0000876 is->resumePlayback();
george8217e92cb2005-01-31 16:01:02 +0000877 delete rfbReader->join();
878 }
879 blankBuffer();
880 newSession(fileName);
881 setSpeed(playbackSpeed);
882 rfbReader = new rfbSessionReader(this);
883 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000884 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8217e92cb2005-01-31 16:01:02 +0000885}
886
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000887void RfbPlayer::setPaused(bool paused) {
888 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000889 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000890 tb.checkButton(ID_PAUSE, true);
891 tb.checkButton(ID_PLAY, false);
892 tb.checkButton(ID_STOP, false);
893 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
894 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000895 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000896 is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000897 tb.checkButton(ID_PLAY, true);
898 tb.checkButton(ID_STOP, false);
899 tb.checkButton(ID_PAUSE, false);
900 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
901 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000902 }
903}
904
george82006f2792005-02-05 07:40:47 +0000905void RfbPlayer::stopPlayback() {
906 setPos(0);
907 is->pausePlayback();
908 tb.checkButton(ID_STOP, true);
909 tb.checkButton(ID_PLAY, false);
910 tb.checkButton(ID_PAUSE, false);
911 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
912 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000913 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000914}
915
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000916void RfbPlayer::setSpeed(double speed) {
917 serverInitTime = serverInitTime * getSpeed() / speed;
918 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000919 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000920}
921
922double RfbPlayer::getSpeed() {
923 return is->getSpeed();
924}
925
926void RfbPlayer::setPos(long pos) {
927 is->setTimeOffset(max(pos, serverInitTime));
928}
929
930long RfbPlayer::getSeekOffset() {
931 return is->getSeekOffset();
932}
933
934bool RfbPlayer::isSeeking() {
935 return is->isSeeking();
936}
937
938bool RfbPlayer::isSeekMode() {
939 return seekMode;
940}
941
942bool RfbPlayer::isPaused() {
943 return is->isPaused();
944}
945
946long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +0000947 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000948}
949
george828a471482005-02-06 07:15:53 +0000950void RfbPlayer::updatePos(long newPos) {
951 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +0000952 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +0000953 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +0000954 newPos /= 1000;
george8244325492005-02-06 07:29:51 +0000955 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +0000956 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +0000957
958 // Update the position of slider
959 if (!sliderDraging) {
960 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +0000961 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
962 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +0000963 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000964}
965
966void RfbPlayer::skipHandshaking() {
967 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
968 is->skip(skipBytes);
969 state_ = RFBSTATE_NORMAL;
970}
971
972void programInfo() {
973 win32::FileVersionInfo inf;
974 _tprintf(_T("%s - %s, Version %s\n"),
975 inf.getVerString(_T("ProductName")),
976 inf.getVerString(_T("FileDescription")),
977 inf.getVerString(_T("FileVersion")));
978 printf("%s\n", buildTime);
979 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
980}
981
982void programUsage() {
983 printf("usage: rfbplayer <options> <filename>\n");
984 printf("Command-line options:\n");
985 printf(" -help - Provide usage information.\n");
986 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
987 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
988 printf(" -pos <ms> - Sets initial time position in the session file,\n");
989 printf(" in milliseconds. Default: 0.\n");
990 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
991 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
992 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
993}
994
995double playbackSpeed = 1.0;
996long initTime = -1;
997bool autoplay = false;
998bool showControls = true;
999char *fileName;
1000bool console = false;
1001bool wrong_param = false;
1002bool print_usage = false;
1003bool acceptBell = false;
1004
1005bool processParams(int argc, char* argv[]) {
1006 for (int i = 1; i < argc; i++) {
1007 if ((strcasecmp(argv[i], "-help") == 0) ||
1008 (strcasecmp(argv[i], "--help") == 0) ||
1009 (strcasecmp(argv[i], "/help") == 0) ||
1010 (strcasecmp(argv[i], "-h") == 0) ||
1011 (strcasecmp(argv[i], "/h") == 0) ||
1012 (strcasecmp(argv[i], "/?") == 0)) {
1013 print_usage = true;
1014 return true;
1015 }
1016
1017 if ((strcasecmp(argv[i], "-speed") == 0) ||
1018 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1019 playbackSpeed = atof(argv[++i]);
1020 if (playbackSpeed <= 0) {
1021 return false;
1022 }
1023 continue;
1024 }
1025
1026 if ((strcasecmp(argv[i], "-pos") == 0) ||
1027 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1028 initTime = atol(argv[++i]);
1029 if (initTime <= 0)
1030 return false;
1031 continue;
1032 }
1033
1034 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1035 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
1036 i++;
1037 if (strcasecmp(argv[i], "yes") == 0) {
1038 autoplay = true;
1039 continue;
1040 }
1041 if (strcasecmp(argv[i], "no") == 0) {
1042 autoplay = false;
1043 continue;
1044 }
1045 return false;
1046 }
1047
1048 if ((strcasecmp(argv[i], "-controls") == 0) ||
1049 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
1050 i++;
1051 if (strcasecmp(argv[i], "yes") == 0) {
1052 showControls = true;
1053 continue;
1054 }
1055 if (strcasecmp(argv[i], "no") == 0) {
1056 showControls = false;
1057 continue;
1058 }
1059 return false;
1060 }
1061
1062 if ((strcasecmp(argv[i], "-bell") == 0) ||
1063 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
1064 i++;
1065 if (strcasecmp(argv[i], "yes") == 0) {
1066 acceptBell = true;
1067 continue;
1068 }
1069 if (strcasecmp(argv[i], "no") == 0) {
1070 acceptBell = false;
1071 continue;
1072 }
1073 return false;
1074 }
1075
1076 if (i != argc - 1)
1077 return false;
1078 }
1079
1080 fileName = strDup(argv[argc-1]);
1081 return true;
1082}
1083
1084//
1085// -=- WinMain
1086//
1087
1088int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1089
1090 // - Process the command-line
1091
1092 int argc = __argc;
1093 char** argv = __argv;
1094 if (argc > 1) {
1095 wrong_param = !processParams(argc, argv);
1096 console = print_usage | wrong_param;
1097 } else {
1098 console = true;
1099 }
1100
1101 if (console) {
1102 AllocConsole();
1103 freopen("CONOUT$","wb",stdout);
1104
1105 programInfo();
1106 if (wrong_param)
1107 printf("Wrong a command line.\n");
1108 else
1109 programUsage();
1110
1111 printf("\nPress Enter/Return key to continue\n");
1112 char c = getch();
1113 FreeConsole();
1114
1115 return 0;
george8267cbcd02005-01-16 15:39:56 +00001116 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001117
1118 // Create the player and the thread which reading the rfb data
1119 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001120 try {
1121 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
1122 showControls, acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001123 } catch (rdr::Exception e) {
1124 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1125 delete player;
1126 return 0;
1127 }
1128
1129 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001130 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001131 MSG msg;
1132 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001133 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1134 TranslateMessage(&msg);
1135 DispatchMessage(&msg);
1136 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137 }
1138
1139 // Wait while the thread destroying and then destroy the player
1140 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001141 if (player) delete player;
1142 } catch (rdr::Exception e) {
1143 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1144 }
1145
1146 return 0;
1147};