blob: 31571ce2c5c9bb7c0b3a6e772608dbcfbdeba176 [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
george82a21d2952005-02-12 11:30:03 +0000629void RfbPlayer::disableTBandMenuItems() {
630 // Disable the menu items
631 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
632 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
633 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
634 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
635 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
636 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
637 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
638 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
640
641 // Disable the toolbar buttons and child controls
642 tb.enableButton(ID_PLAY, false);
643 tb.enableButton(ID_PAUSE, false);
644 tb.enableButton(ID_STOP, false);
645 tb.enableButton(ID_FULLSCREEN, false);
646 EnableWindow(posTrackBar, false);
647 EnableWindow(speedEdit, false);
648}
649
george82f5043162005-02-12 11:37:18 +0000650void RfbPlayer::enableTBandMenuItems() {
651 // Enable the menu items
652 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
653 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
654 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
655 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
656 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
657 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
658 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
659 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
660 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
661
662 // Enable the toolbar buttons and child controls
663 tb.enableButton(ID_PLAY, true);
664 tb.enableButton(ID_PAUSE, true);
665 tb.enableButton(ID_STOP, true);
666 tb.enableButton(ID_FULLSCREEN, true);
667 EnableWindow(posTrackBar, true);
668 EnableWindow(speedEdit, true);
669}
670
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000671void RfbPlayer::setVisible(bool visible) {
672 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
673 if (visible) {
674 // When the window becomes visible, make it active
675 SetForegroundWindow(getMainHandle());
676 SetActiveWindow(getMainHandle());
677 }
678}
679
680void RfbPlayer::setTitle(const char *title) {
681 char _title[256];
682 strcpy(_title, AppName);
683 strcat(_title, " - ");
684 strcat(_title, title);
685 SetWindowText(getMainHandle(), _title);
686}
687
688void RfbPlayer::setFrameSize(int width, int height) {
689 // Calculate and set required size for main window
690 RECT r = {0, 0, width, height};
691 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
692 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
693 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
694 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
695 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
696 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
697
698 // Enable/disable scrollbars as appropriate
699 calculateScrollBars();
700}
701
702void RfbPlayer::calculateScrollBars() {
703 // Calculate the required size of window
704 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
705 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
706 DWORD old_style;
707 RECT r;
708 SetRect(&r, 0, 0, buffer->width(), buffer->height());
709 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
710 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
711
712 // Work out whether scroll bars are required
713 do {
714 old_style = style;
715
716 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
717 style |= WS_HSCROLL;
718 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
719 }
720 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
721 style |= WS_VSCROLL;
722 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
723 }
724 } while (style != old_style);
725
726 // Tell Windows to update the window style & cached settings
727 if (style != current_style) {
728 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
729 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
730 }
731
732 // Update the scroll settings
733 SCROLLINFO si;
734 if (style & WS_VSCROLL) {
735 si.cbSize = sizeof(si);
736 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
737 si.nMin = 0;
738 si.nMax = buffer->height();
739 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
740 maxscrolloffset.y = max(0, si.nMax-si.nPage);
741 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
742 si.nPos = scrolloffset.y;
743 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
744 }
745 if (style & WS_HSCROLL) {
746 si.cbSize = sizeof(si);
747 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
748 si.nMin = 0;
749 si.nMax = buffer->width();
750 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
751 maxscrolloffset.x = max(0, si.nMax-si.nPage);
752 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
753 si.nPos = scrolloffset.x;
754 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
755 }
756}
757
758bool RfbPlayer::setViewportOffset(const Point& tl) {
759/* ***
760 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
761 max(0, min(maxscrolloffset.y, tl.y)));
762 */
763 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
764 max(0, min(tl.y, buffer->height()-client_size.height())));
765 Point delta = np.translate(scrolloffset.negate());
766 if (!np.equals(scrolloffset)) {
767 scrolloffset = np;
768 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
769 UpdateWindow(getFrameHandle());
770 return true;
771 }
772 return false;
773}
774
775void RfbPlayer::close(const char* reason) {
776 setVisible(false);
777 if (reason) {
778 vlog.info("closing - %s", reason);
779 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
780 }
781 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
782}
783
784void RfbPlayer::blankBuffer() {
785 fillRect(buffer->getRect(), 0);
786}
787
788void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000789 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000790 blankBuffer();
791 newSession(fileName);
792 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000793 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000794 if (paused) is->pausePlayback();
795 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000796}
797
798void RfbPlayer::processMsg() {
799 static long update_time = GetTickCount();
800 try {
george828a471482005-02-06 07:15:53 +0000801 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
802 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000803 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000804 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000805 update_time = GetTickCount();
806 }
807 RfbProto::processMsg();
808 } catch (rdr::Exception e) {
809 if (strcmp(e.str(), "[End Of File]") == 0) {
810 rewind();
george8231a36332005-02-06 17:27:34 +0000811 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000812 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000813 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000814 return;
815 }
816 // It's a special exception to perform backward seeking.
817 // We only rewind the stream and seek the offset
818 if (strcmp(e.str(), "[REWIND]") == 0) {
819 long initTime = getSeekOffset();
820 rewind();
821 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000822 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000823 } else {
824 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
825 return;
826 }
827 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000828}
829
830void RfbPlayer::serverInit() {
831 RfbProto::serverInit();
832
833 // Save the server init time for using in setPos()
834 serverInitTime = getTimeOffset() / getSpeed();
835
836 // Resize the backing buffer
837 buffer->setSize(cp.width, cp.height);
838
839 // Check on the true colour mode
840 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000841 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000842
843 // Set the session pixel format
844 buffer->setPF(cp.pf());
845
846 // If the window is not maximised then resize it
847 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
848 setFrameSize(cp.width, cp.height);
849
850 // Set the window title and show it
851 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000852
george82d4d69e62005-02-05 09:23:18 +0000853 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000854 sessionTimeMs = calculateSessionTime(fileName);
855 sprintf(fullSessionTime, "%.2um:%.2us",
856 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000857 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000858 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
859 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000860 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000861
george82006f2792005-02-05 07:40:47 +0000862 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000863}
864
865void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
866 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
867 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
868/* int i;
869 for (i=0;i<count;i++) {
870 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
871 }
872 // *** change to 0, 256?
873 refreshWindowPalette(first, count);
874 palette_changed = true;
875 InvalidateRect(getFrameHandle(), 0, FALSE);*/
876}
877
878void RfbPlayer::bell() {
879 if (acceptBell)
880 MessageBeep(-1);
881}
882
883void RfbPlayer::serverCutText(const char* str, int len) {
884 if (cutText != NULL)
885 delete [] cutText;
886 cutText = new char[len + 1];
887 memcpy(cutText, str, len);
888 cutText[len] = '\0';
889}
890
891void RfbPlayer::frameBufferUpdateEnd() {
892};
893
894void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
895}
896
897void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
898}
899
900
901void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
902 buffer->fillRect(r, pix);
903 invalidateBufferRect(r);
904}
905
906void RfbPlayer::imageRect(const Rect& r, void* pixels) {
907 buffer->imageRect(r, pixels);
908 invalidateBufferRect(r);
909}
910
911void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
912 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
913 invalidateBufferRect(r);
914}
915
916bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
917 Rect rect = bufferToClient(crect);
918 if (rect.intersect(client_size).is_empty()) return false;
919 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
920 InvalidateRect(getFrameHandle(), &invalid, FALSE);
921 return true;
922}
923
george8257f13522005-02-05 08:48:22 +0000924long RfbPlayer::calculateSessionTime(char *filename) {
925 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000926 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000927 try {
928 while (TRUE) {
929 sessionFile.skip(1024);
930 }
931 } catch (rdr::Exception e) {
932 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000933 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000934 } else {
935 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
936 return 0;
937 }
938 }
939 return 0;
940}
941
george8217e92cb2005-01-31 16:01:02 +0000942void RfbPlayer::openSessionFile(char *_fileName) {
943 fileName = strDup(_fileName);
944
945 // Close the previous reading thread
946 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000947 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000948 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000949 }
950 blankBuffer();
951 newSession(fileName);
952 setSpeed(playbackSpeed);
953 rfbReader = new rfbSessionReader(this);
954 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000955 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8217e92cb2005-01-31 16:01:02 +0000956}
957
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000958void RfbPlayer::setPaused(bool paused) {
959 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000960 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000961 tb.checkButton(ID_PAUSE, true);
962 tb.checkButton(ID_PLAY, false);
963 tb.checkButton(ID_STOP, false);
964 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
965 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000966 } else {
george825beb62a2005-02-09 13:04:32 +0000967 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000968 tb.checkButton(ID_PLAY, true);
969 tb.checkButton(ID_STOP, false);
970 tb.checkButton(ID_PAUSE, false);
971 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
972 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000973 }
974}
975
george82006f2792005-02-05 07:40:47 +0000976void RfbPlayer::stopPlayback() {
977 setPos(0);
george825beb62a2005-02-09 13:04:32 +0000978 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000979 tb.checkButton(ID_STOP, true);
980 tb.checkButton(ID_PLAY, false);
981 tb.checkButton(ID_PAUSE, false);
982 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
983 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000984 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000985}
986
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000987void RfbPlayer::setSpeed(double speed) {
988 serverInitTime = serverInitTime * getSpeed() / speed;
989 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000990 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000991}
992
993double RfbPlayer::getSpeed() {
994 return is->getSpeed();
995}
996
997void RfbPlayer::setPos(long pos) {
998 is->setTimeOffset(max(pos, serverInitTime));
999}
1000
1001long RfbPlayer::getSeekOffset() {
1002 return is->getSeekOffset();
1003}
1004
1005bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001006 if (is) return is->isSeeking();
1007 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001008}
1009
1010bool RfbPlayer::isSeekMode() {
1011 return seekMode;
1012}
1013
1014bool RfbPlayer::isPaused() {
1015 return is->isPaused();
1016}
1017
1018long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001019 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001020}
1021
george828a471482005-02-06 07:15:53 +00001022void RfbPlayer::updatePos(long newPos) {
1023 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001024 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +00001025 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +00001026 newPos /= 1000;
george8244325492005-02-06 07:29:51 +00001027 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001028 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001029
1030 // Update the position of slider
1031 if (!sliderDraging) {
1032 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +00001033 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
1034 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +00001035 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001036}
1037
1038void RfbPlayer::skipHandshaking() {
1039 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1040 is->skip(skipBytes);
1041 state_ = RFBSTATE_NORMAL;
1042}
1043
1044void programInfo() {
1045 win32::FileVersionInfo inf;
1046 _tprintf(_T("%s - %s, Version %s\n"),
1047 inf.getVerString(_T("ProductName")),
1048 inf.getVerString(_T("FileDescription")),
1049 inf.getVerString(_T("FileVersion")));
1050 printf("%s\n", buildTime);
1051 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1052}
1053
1054void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001055 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001056}
1057
1058double playbackSpeed = 1.0;
1059long initTime = -1;
1060bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001061char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001062bool print_usage = false;
1063bool acceptBell = false;
1064
1065bool processParams(int argc, char* argv[]) {
1066 for (int i = 1; i < argc; i++) {
1067 if ((strcasecmp(argv[i], "-help") == 0) ||
1068 (strcasecmp(argv[i], "--help") == 0) ||
1069 (strcasecmp(argv[i], "/help") == 0) ||
1070 (strcasecmp(argv[i], "-h") == 0) ||
1071 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001072 (strcasecmp(argv[i], "/?") == 0) ||
1073 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001074 print_usage = true;
1075 return true;
1076 }
1077
1078 if ((strcasecmp(argv[i], "-speed") == 0) ||
1079 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1080 playbackSpeed = atof(argv[++i]);
1081 if (playbackSpeed <= 0) {
1082 return false;
1083 }
1084 continue;
1085 }
1086
1087 if ((strcasecmp(argv[i], "-pos") == 0) ||
1088 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1089 initTime = atol(argv[++i]);
1090 if (initTime <= 0)
1091 return false;
1092 continue;
1093 }
1094
1095 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1096 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001097 autoplay = true;
1098 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001099 }
1100
1101 if ((strcasecmp(argv[i], "-bell") == 0) ||
1102 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001103 acceptBell = true;
1104 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001105 }
1106
1107 if (i != argc - 1)
1108 return false;
1109 }
1110
1111 fileName = strDup(argv[argc-1]);
1112 return true;
1113}
1114
1115//
1116// -=- WinMain
1117//
1118
1119int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1120
1121 // - Process the command-line
1122
1123 int argc = __argc;
1124 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001125 if ((argc > 1) && (!processParams(argc, argv))) {
1126 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1127 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001128 }
george82e6883de2005-02-08 14:42:12 +00001129
1130 if (print_usage) {
1131 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001132 return 0;
george8267cbcd02005-01-16 15:39:56 +00001133 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001134
george82e6883de2005-02-08 14:42:12 +00001135 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001136 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137 try {
1138 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001139 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001140 } catch (rdr::Exception e) {
1141 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1142 delete player;
1143 return 0;
1144 }
1145
1146 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001147 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001148 MSG msg;
1149 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001150 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1151 TranslateMessage(&msg);
1152 DispatchMessage(&msg);
1153 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001154 }
1155
george82e6883de2005-02-08 14:42:12 +00001156 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001157 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001158 if (player) delete player;
1159 } catch (rdr::Exception e) {
1160 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1161 }
1162
1163 return 0;
1164};