blob: 1a6dbfd24dac6c57fb1f82dba808d5eaeeb033e1 [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),
george8231a36332005-02-06 17:27:34 +0000208 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
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;
george8231a36332005-02-06 17:27:34 +0000319 case ID_LOOP:
320 loopPlayback = !loopPlayback;
321 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
322 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
323 break;
george824ea27f62005-01-29 15:03:06 +0000324 case ID_RETURN:
325 // Update the speed if return pressed in speedEdit
326 if (speedEdit == GetFocus()) {
327 char speedStr[20], *stopStr;
328 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
329 double speed = strtod(speedStr, &stopStr);
330 if (speed > 0) {
331 speed = min(MAX_SPEED, speed);
332 // Update speedUpDown position
333 SendMessage(speedUpDown, UDM_SETPOS,
334 0, MAKELONG((short)(speed / 0.5), 0));
335 } else {
336 speed = getSpeed();
337 }
338 setSpeed(speed);
339 sprintf(speedStr, "%.2f", speed);
340 SetWindowText(speedEdit, speedStr);
341 }
342 break;
george8201aa6732005-02-06 17:13:03 +0000343 case ID_EXIT:
344 is->resumePlayback();
345 PostQuitMessage(0);
346 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000347 }
348 break;
349
350 // Update frame's window size and add scrollbars if required
351
352 case WM_SIZE:
353 {
george82d070c692005-01-19 16:44:04 +0000354
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000355 Point old_offset = bufferToClient(Point(0, 0));
356
357 // Update the cached sizing information
358 RECT r;
359 GetClientRect(getMainHandle(), &r);
360 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
361 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
362
363 GetWindowRect(getFrameHandle(), &r);
364 window_size = Rect(r.left, r.top, r.right, r.bottom);
365 GetClientRect(getFrameHandle(), &r);
366 client_size = Rect(r.left, r.top, r.right, r.bottom);
367
368 // Determine whether scrollbars are required
369 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000370
371 // Resize the ToolBar
372 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000373
374 // Redraw if required
375 if (!old_offset.equals(bufferToClient(Point(0, 0))))
376 InvalidateRect(getFrameHandle(), 0, TRUE);
377 }
378 break;
george828a471482005-02-06 07:15:53 +0000379
380 // Process messages from posTrackBar
381
382 case WM_HSCROLL:
383 {
384 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
385 Pos *= sliderStepMs;
386
387 switch (LOWORD(wParam)) {
388 case TB_PAGEUP:
389 case TB_PAGEDOWN:
390 case TB_LINEUP:
391 case TB_LINEDOWN:
392 case TB_THUMBTRACK:
393 sliderDraging = true;
394 updatePos(Pos);
395 return 0;
396 case TB_ENDTRACK:
397 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000398 sliderDraging = false;
399 return 0;
400 default:
401 break;
402 }
403 }
404 break;
george829e6e6cc2005-01-29 13:12:05 +0000405
406 case WM_NOTIFY:
407 switch (((NMHDR*)lParam)->code) {
408 case UDN_DELTAPOS:
409 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000410 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000411 char speedStr[20] = "\0";
412 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
413 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
414 double speed;
415
george824ea27f62005-01-29 15:03:06 +0000416 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000417 if (upDown->iDelta > 0) {
418 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
419 } else {
george824ea27f62005-01-29 15:03:06 +0000420 // It's need to round the UpDown position
421 if ((upDown->iPos * 0.5) != getSpeed()) {
422 upDown->iDelta = 0;
423 lResult = TRUE;
424 }
george829e6e6cc2005-01-29 13:12:05 +0000425 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
426 }
427 _gcvt(speed, 5, speedStr);
428 sprintf(speedStr, "%.2f", speed);
429 SetWindowText(speedEdit, speedStr);
430 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000431 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000432 }
george824ea27f62005-01-29 15:03:06 +0000433 }
george829e6e6cc2005-01-29 13:12:05 +0000434 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000435
436 case WM_CLOSE:
437 vlog.debug("WM_CLOSE %x", getMainHandle());
438 PostQuitMessage(0);
439 break;
440 }
441
442 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
443}
444
445LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
446 switch (msg) {
447
448 case WM_PAINT:
449 {
450 if (is->isSeeking()) {
451 seekMode = true;
452 return 0;
453 } else {
454 if (seekMode) {
455 seekMode = false;
456 InvalidateRect(getFrameHandle(), 0, true);
457 UpdateWindow(getFrameHandle());
458 return 0;
459 }
460 }
461
462 PAINTSTRUCT ps;
463 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
464 if (!paintDC)
465 throw SystemException("unable to BeginPaint", GetLastError());
466 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
467
468 if (!pr.is_empty()) {
469
470 if (buffer->bitmap) {
471
472 // Get device context
473 BitmapDC bitmapDC(paintDC, buffer->bitmap);
474
475 // Blit the border if required
476 Rect bufpos = bufferToClient(buffer->getRect());
477 if (!pr.enclosed_by(bufpos)) {
478 vlog.debug("draw border");
479 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
480 RECT r;
481 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
482 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
483 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
484 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
485 }
486
487 // Do the blit
488 Point buf_pos = clientToBuffer(pr.tl);
489 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
490 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
491 throw SystemException("unable to BitBlt to window", GetLastError());
492
493 } else {
494 // Blit a load of black
495 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
496 0, 0, 0, BLACKNESS))
497 throw SystemException("unable to BitBlt to blank window", GetLastError());
498 }
499 }
500 EndPaint(getFrameHandle(), &ps);
501 }
502 return 0;
503
504 case WM_VSCROLL:
505 case WM_HSCROLL:
506 {
507 Point delta;
508 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
509
510 switch (LOWORD(wParam)) {
511 case SB_PAGEUP: newpos -= 50; break;
512 case SB_PAGEDOWN: newpos += 50; break;
513 case SB_LINEUP: newpos -= 5; break;
514 case SB_LINEDOWN: newpos += 5; break;
515 case SB_THUMBTRACK:
516 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
517 default: vlog.info("received unknown scroll message");
518 };
519
520 if (msg == WM_HSCROLL)
521 setViewportOffset(Point(newpos, scrolloffset.y));
522 else
523 setViewportOffset(Point(scrolloffset.x, newpos));
524
525 SCROLLINFO si;
526 si.cbSize = sizeof(si);
527 si.fMask = SIF_POS;
528 si.nPos = newpos;
529 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
530 }
531 break;
532 }
533
534 return DefWindowProc(hwnd, msg, wParam, lParam);
535}
536
537void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
538 bool _autoplay = false, bool _showControls = true) {
539 showControls = _showControls;
540 autoplay = _autoplay;
541 playbackSpeed = _playbackSpeed;
542 initTime = _initTime;
543}
544
545void RfbPlayer::applyOptions() {
546 if (initTime >= 0)
547 setPos(initTime);
548 setSpeed(playbackSpeed);
549 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000550}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000551
george82d070c692005-01-19 16:44:04 +0000552void RfbPlayer::createToolBar(HWND parentHwnd) {
553 RECT tRect;
554 InitCommonControls();
555
556 tb.create(ID_TOOLBAR, parentHwnd);
557 tb.addBitmap(4, IDB_TOOLBAR);
558
559 // Create the control buttons
560 tb.addButton(0, ID_PLAY);
561 tb.addButton(1, ID_PAUSE);
562 tb.addButton(2, ID_STOP);
563 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
564 tb.addButton(3, ID_FULLSCREEN);
565 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
566
567 // Create the static control for the time output
568 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
569 tb.getButtonRect(6, &tRect);
570 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
571 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
572 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
573 GetModuleHandle(0), 0);
574 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
575
576 // Create the trackbar control for the time position
577 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
578 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000579 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000580 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
581 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
582 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
583 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000584 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000585 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
586
587 // Create the label with "Speed:" caption
588 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
589 tb.getButtonRect(10, &tRect);
590 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
591 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
592 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
593
594 // Create the edit control and the spin for the speed managing
595 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
596 tb.getButtonRect(11, &tRect);
597 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
598 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
599 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
600 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
601 // It's need to send notify messages to toolbar parent window
602 SetParent(speedEdit, tb.getHandle());
603
604 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
605 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000606 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000607}
608
609void RfbPlayer::setVisible(bool visible) {
610 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
611 if (visible) {
612 // When the window becomes visible, make it active
613 SetForegroundWindow(getMainHandle());
614 SetActiveWindow(getMainHandle());
615 }
616}
617
618void RfbPlayer::setTitle(const char *title) {
619 char _title[256];
620 strcpy(_title, AppName);
621 strcat(_title, " - ");
622 strcat(_title, title);
623 SetWindowText(getMainHandle(), _title);
624}
625
626void RfbPlayer::setFrameSize(int width, int height) {
627 // Calculate and set required size for main window
628 RECT r = {0, 0, width, height};
629 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
630 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
631 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
632 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
633 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
634 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
635
636 // Enable/disable scrollbars as appropriate
637 calculateScrollBars();
638}
639
640void RfbPlayer::calculateScrollBars() {
641 // Calculate the required size of window
642 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
643 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
644 DWORD old_style;
645 RECT r;
646 SetRect(&r, 0, 0, buffer->width(), buffer->height());
647 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
648 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
649
650 // Work out whether scroll bars are required
651 do {
652 old_style = style;
653
654 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
655 style |= WS_HSCROLL;
656 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
657 }
658 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
659 style |= WS_VSCROLL;
660 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
661 }
662 } while (style != old_style);
663
664 // Tell Windows to update the window style & cached settings
665 if (style != current_style) {
666 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
667 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
668 }
669
670 // Update the scroll settings
671 SCROLLINFO si;
672 if (style & WS_VSCROLL) {
673 si.cbSize = sizeof(si);
674 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
675 si.nMin = 0;
676 si.nMax = buffer->height();
677 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
678 maxscrolloffset.y = max(0, si.nMax-si.nPage);
679 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
680 si.nPos = scrolloffset.y;
681 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
682 }
683 if (style & WS_HSCROLL) {
684 si.cbSize = sizeof(si);
685 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
686 si.nMin = 0;
687 si.nMax = buffer->width();
688 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
689 maxscrolloffset.x = max(0, si.nMax-si.nPage);
690 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
691 si.nPos = scrolloffset.x;
692 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
693 }
694}
695
696bool RfbPlayer::setViewportOffset(const Point& tl) {
697/* ***
698 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
699 max(0, min(maxscrolloffset.y, tl.y)));
700 */
701 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
702 max(0, min(tl.y, buffer->height()-client_size.height())));
703 Point delta = np.translate(scrolloffset.negate());
704 if (!np.equals(scrolloffset)) {
705 scrolloffset = np;
706 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
707 UpdateWindow(getFrameHandle());
708 return true;
709 }
710 return false;
711}
712
713void RfbPlayer::close(const char* reason) {
714 setVisible(false);
715 if (reason) {
716 vlog.info("closing - %s", reason);
717 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
718 }
719 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
720}
721
722void RfbPlayer::blankBuffer() {
723 fillRect(buffer->getRect(), 0);
724}
725
726void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000727 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000728 blankBuffer();
729 newSession(fileName);
730 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000731 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000732 if (paused) is->pausePlayback();
733 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000734}
735
736void RfbPlayer::processMsg() {
737 static long update_time = GetTickCount();
738 try {
george828a471482005-02-06 07:15:53 +0000739 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
740 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000741 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000742 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000743 update_time = GetTickCount();
744 }
745 RfbProto::processMsg();
746 } catch (rdr::Exception e) {
747 if (strcmp(e.str(), "[End Of File]") == 0) {
748 rewind();
george8231a36332005-02-06 17:27:34 +0000749 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000750 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000751 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000752 return;
753 }
754 // It's a special exception to perform backward seeking.
755 // We only rewind the stream and seek the offset
756 if (strcmp(e.str(), "[REWIND]") == 0) {
757 long initTime = getSeekOffset();
758 rewind();
759 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000760 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000761 } else {
762 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
763 return;
764 }
765 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000766}
767
768void RfbPlayer::serverInit() {
769 RfbProto::serverInit();
770
771 // Save the server init time for using in setPos()
772 serverInitTime = getTimeOffset() / getSpeed();
773
774 // Resize the backing buffer
775 buffer->setSize(cp.width, cp.height);
776
777 // Check on the true colour mode
778 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000779 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000780
781 // Set the session pixel format
782 buffer->setPF(cp.pf());
783
784 // If the window is not maximised then resize it
785 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
786 setFrameSize(cp.width, cp.height);
787
788 // Set the window title and show it
789 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000790
george82d4d69e62005-02-05 09:23:18 +0000791 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000792 sessionTimeMs = calculateSessionTime(fileName);
793 sprintf(fullSessionTime, "%.2um:%.2us",
794 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000795 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000796 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
797 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000798 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000799
george82006f2792005-02-05 07:40:47 +0000800 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000801}
802
803void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
804 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
805 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
806/* int i;
807 for (i=0;i<count;i++) {
808 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
809 }
810 // *** change to 0, 256?
811 refreshWindowPalette(first, count);
812 palette_changed = true;
813 InvalidateRect(getFrameHandle(), 0, FALSE);*/
814}
815
816void RfbPlayer::bell() {
817 if (acceptBell)
818 MessageBeep(-1);
819}
820
821void RfbPlayer::serverCutText(const char* str, int len) {
822 if (cutText != NULL)
823 delete [] cutText;
824 cutText = new char[len + 1];
825 memcpy(cutText, str, len);
826 cutText[len] = '\0';
827}
828
829void RfbPlayer::frameBufferUpdateEnd() {
830};
831
832void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
833}
834
835void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
836}
837
838
839void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
840 buffer->fillRect(r, pix);
841 invalidateBufferRect(r);
842}
843
844void RfbPlayer::imageRect(const Rect& r, void* pixels) {
845 buffer->imageRect(r, pixels);
846 invalidateBufferRect(r);
847}
848
849void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
850 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
851 invalidateBufferRect(r);
852}
853
854bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
855 Rect rect = bufferToClient(crect);
856 if (rect.intersect(client_size).is_empty()) return false;
857 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
858 InvalidateRect(getFrameHandle(), &invalid, FALSE);
859 return true;
860}
861
george8257f13522005-02-05 08:48:22 +0000862long RfbPlayer::calculateSessionTime(char *filename) {
863 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000864 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000865 try {
866 while (TRUE) {
867 sessionFile.skip(1024);
868 }
869 } catch (rdr::Exception e) {
870 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000871 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000872 } else {
873 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
874 return 0;
875 }
876 }
877 return 0;
878}
879
george8217e92cb2005-01-31 16:01:02 +0000880void RfbPlayer::openSessionFile(char *_fileName) {
881 fileName = strDup(_fileName);
882
883 // Close the previous reading thread
884 if (rfbReader) {
george826e51fcc2005-02-06 13:30:49 +0000885 is->resumePlayback();
george8217e92cb2005-01-31 16:01:02 +0000886 delete rfbReader->join();
887 }
888 blankBuffer();
889 newSession(fileName);
890 setSpeed(playbackSpeed);
891 rfbReader = new rfbSessionReader(this);
892 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000893 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8217e92cb2005-01-31 16:01:02 +0000894}
895
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000896void RfbPlayer::setPaused(bool paused) {
897 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000898 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000899 tb.checkButton(ID_PAUSE, true);
900 tb.checkButton(ID_PLAY, false);
901 tb.checkButton(ID_STOP, false);
902 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
903 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000904 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000905 is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000906 tb.checkButton(ID_PLAY, true);
907 tb.checkButton(ID_STOP, false);
908 tb.checkButton(ID_PAUSE, false);
909 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
910 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000911 }
912}
913
george82006f2792005-02-05 07:40:47 +0000914void RfbPlayer::stopPlayback() {
915 setPos(0);
916 is->pausePlayback();
917 tb.checkButton(ID_STOP, true);
918 tb.checkButton(ID_PLAY, false);
919 tb.checkButton(ID_PAUSE, false);
920 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
921 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000922 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000923}
924
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000925void RfbPlayer::setSpeed(double speed) {
926 serverInitTime = serverInitTime * getSpeed() / speed;
927 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000928 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000929}
930
931double RfbPlayer::getSpeed() {
932 return is->getSpeed();
933}
934
935void RfbPlayer::setPos(long pos) {
936 is->setTimeOffset(max(pos, serverInitTime));
937}
938
939long RfbPlayer::getSeekOffset() {
940 return is->getSeekOffset();
941}
942
943bool RfbPlayer::isSeeking() {
944 return is->isSeeking();
945}
946
947bool RfbPlayer::isSeekMode() {
948 return seekMode;
949}
950
951bool RfbPlayer::isPaused() {
952 return is->isPaused();
953}
954
955long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +0000956 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000957}
958
george828a471482005-02-06 07:15:53 +0000959void RfbPlayer::updatePos(long newPos) {
960 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +0000961 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +0000962 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +0000963 newPos /= 1000;
george8244325492005-02-06 07:29:51 +0000964 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +0000965 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +0000966
967 // Update the position of slider
968 if (!sliderDraging) {
969 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +0000970 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
971 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +0000972 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000973}
974
975void RfbPlayer::skipHandshaking() {
976 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
977 is->skip(skipBytes);
978 state_ = RFBSTATE_NORMAL;
979}
980
981void programInfo() {
982 win32::FileVersionInfo inf;
983 _tprintf(_T("%s - %s, Version %s\n"),
984 inf.getVerString(_T("ProductName")),
985 inf.getVerString(_T("FileDescription")),
986 inf.getVerString(_T("FileVersion")));
987 printf("%s\n", buildTime);
988 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
989}
990
991void programUsage() {
992 printf("usage: rfbplayer <options> <filename>\n");
993 printf("Command-line options:\n");
994 printf(" -help - Provide usage information.\n");
995 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
996 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
997 printf(" -pos <ms> - Sets initial time position in the session file,\n");
998 printf(" in milliseconds. Default: 0.\n");
999 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
1000 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
1001 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
1002}
1003
1004double playbackSpeed = 1.0;
1005long initTime = -1;
1006bool autoplay = false;
1007bool showControls = true;
1008char *fileName;
1009bool console = false;
1010bool wrong_param = false;
1011bool print_usage = false;
1012bool acceptBell = false;
1013
1014bool processParams(int argc, char* argv[]) {
1015 for (int i = 1; i < argc; i++) {
1016 if ((strcasecmp(argv[i], "-help") == 0) ||
1017 (strcasecmp(argv[i], "--help") == 0) ||
1018 (strcasecmp(argv[i], "/help") == 0) ||
1019 (strcasecmp(argv[i], "-h") == 0) ||
1020 (strcasecmp(argv[i], "/h") == 0) ||
1021 (strcasecmp(argv[i], "/?") == 0)) {
1022 print_usage = true;
1023 return true;
1024 }
1025
1026 if ((strcasecmp(argv[i], "-speed") == 0) ||
1027 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1028 playbackSpeed = atof(argv[++i]);
1029 if (playbackSpeed <= 0) {
1030 return false;
1031 }
1032 continue;
1033 }
1034
1035 if ((strcasecmp(argv[i], "-pos") == 0) ||
1036 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1037 initTime = atol(argv[++i]);
1038 if (initTime <= 0)
1039 return false;
1040 continue;
1041 }
1042
1043 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1044 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
1045 i++;
1046 if (strcasecmp(argv[i], "yes") == 0) {
1047 autoplay = true;
1048 continue;
1049 }
1050 if (strcasecmp(argv[i], "no") == 0) {
1051 autoplay = false;
1052 continue;
1053 }
1054 return false;
1055 }
1056
1057 if ((strcasecmp(argv[i], "-controls") == 0) ||
1058 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
1059 i++;
1060 if (strcasecmp(argv[i], "yes") == 0) {
1061 showControls = true;
1062 continue;
1063 }
1064 if (strcasecmp(argv[i], "no") == 0) {
1065 showControls = false;
1066 continue;
1067 }
1068 return false;
1069 }
1070
1071 if ((strcasecmp(argv[i], "-bell") == 0) ||
1072 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
1073 i++;
1074 if (strcasecmp(argv[i], "yes") == 0) {
1075 acceptBell = true;
1076 continue;
1077 }
1078 if (strcasecmp(argv[i], "no") == 0) {
1079 acceptBell = false;
1080 continue;
1081 }
1082 return false;
1083 }
1084
1085 if (i != argc - 1)
1086 return false;
1087 }
1088
1089 fileName = strDup(argv[argc-1]);
1090 return true;
1091}
1092
1093//
1094// -=- WinMain
1095//
1096
1097int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1098
1099 // - Process the command-line
1100
1101 int argc = __argc;
1102 char** argv = __argv;
1103 if (argc > 1) {
1104 wrong_param = !processParams(argc, argv);
1105 console = print_usage | wrong_param;
1106 } else {
1107 console = true;
1108 }
1109
1110 if (console) {
1111 AllocConsole();
1112 freopen("CONOUT$","wb",stdout);
1113
1114 programInfo();
1115 if (wrong_param)
1116 printf("Wrong a command line.\n");
1117 else
1118 programUsage();
1119
1120 printf("\nPress Enter/Return key to continue\n");
1121 char c = getch();
1122 FreeConsole();
1123
1124 return 0;
george8267cbcd02005-01-16 15:39:56 +00001125 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001126
1127 // Create the player and the thread which reading the rfb data
1128 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001129 try {
1130 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
1131 showControls, acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001132 } catch (rdr::Exception e) {
1133 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1134 delete player;
1135 return 0;
1136 }
1137
1138 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001139 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001140 MSG msg;
1141 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001142 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1143 TranslateMessage(&msg);
1144 DispatchMessage(&msg);
1145 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001146 }
1147
1148 // Wait while the thread destroying and then destroy the player
1149 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001150 if (player) delete player;
1151 } catch (rdr::Exception e) {
1152 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1153 }
1154
1155 return 0;
1156};