blob: 7cd1fe35185ef4d316e36706f3cf4e373289d02b [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
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000048
george82d070c692005-01-19 16:44:04 +000049#define ID_TOOLBAR 500
50#define ID_PLAY 510
51#define ID_PAUSE 520
52#define ID_TIME_STATIC 530
53#define ID_SPEED_STATIC 540
54#define ID_SPEED_EDIT 550
55#define ID_POS_TRACKBAR 560
56#define ID_SPEED_UPDOWN 570
57
58
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000059//
60// -=- RfbPlayerClass
61
62//
63// Window class used as the basis for RfbPlayer instance
64//
65
66class RfbPlayerClass {
67public:
68 RfbPlayerClass();
69 ~RfbPlayerClass();
70 ATOM classAtom;
71 HINSTANCE instance;
72};
73
74LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
75 LRESULT result;
76
77 if (msg == WM_CREATE)
78 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
79 else if (msg == WM_DESTROY) {
80 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
george8267cbcd02005-01-16 15:39:56 +000081 _this->fRun = false;
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),
george8267cbcd02005-01-16 15:39:56 +0000205 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName), fRun(true),
george82d070c692005-01-19 16:44:04 +0000206 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), speedTrackBar(0),
207 speedUpDown(0), acceptBell(_acceptBell) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000208
209 if (showControls)
george82d070c692005-01-19 16:44:04 +0000210 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000211 else
212 CTRL_BAR_HEIGHT = 0;
213
george823c8fbbf2005-01-24 11:09:08 +0000214 // Reset the full session time
215 strcpy(fullSessionTime, "00m:00s");
216
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000217 // Create the main window
218 const TCHAR* name = _T("RfbPlayer");
219 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000220 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000221 if (!mainHwnd) {
222 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
223 }
224 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
225
226 // Create the backing buffer
227 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000228 setVisible(true);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000229}
230
231RfbPlayer::~RfbPlayer() {
232 vlog.debug("~RfbPlayer");
233 if (mainHwnd) {
234 setVisible(false);
235 DestroyWindow(mainHwnd);
236 mainHwnd = 0;
237 }
238 delete buffer;
239 delete cutText;
240 vlog.debug("~RfbPlayer done");
241}
242
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000243LRESULT
244RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
george825c13c662005-01-27 14:48:23 +0000245 static HMENU hmenu; // handle to main menu
246
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000247 switch (msg) {
248
249 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000250
251 case WM_CREATE:
252 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000253 // Create the frame window
254 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
255 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
256 hwnd, 0, frameClass.instance, this);
257
george82d070c692005-01-19 16:44:04 +0000258 createToolBar(hwnd);
259
george825c13c662005-01-27 14:48:23 +0000260 hmenu = GetMenu(hwnd);
261
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000262 return 0;
263 }
264
george827214b822004-12-12 07:02:51 +0000265 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000266
267 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000268 switch (LOWORD(wParam)) {
269 case ID_PLAY:
270 setPaused(false);
271 tb.checkButton(ID_PLAY, true);
272 tb.checkButton(ID_STOP, false);
273 tb.checkButton(ID_PAUSE, false);
274 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_CHECKED);
275 CheckMenuItem(hmenu, ID_STOP, MF_UNCHECKED);
276 break;
277 case ID_PAUSE:
278 setPaused(true);
279 tb.checkButton(ID_PAUSE, true);
280 tb.checkButton(ID_PLAY, false);
281 tb.checkButton(ID_STOP, false);
282 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_CHECKED);
283 CheckMenuItem(hmenu, ID_STOP, MF_UNCHECKED);
284 break;
285 case ID_STOP:
286 if (getTimeOffset() != 0) {
287 setPaused(true);
288 setPos(0);
289 }
290 tb.checkButton(ID_STOP, true);
291 tb.checkButton(ID_PLAY, false);
292 tb.checkButton(ID_PAUSE, false);
293 CheckMenuItem(hmenu, ID_STOP, MF_CHECKED);
294 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_UNCHECKED);
295 break;
296 case ID_PLAYPAUSE:
297 if (isPaused()) {
298 setPaused(false);
299 tb.checkButton(ID_PLAY, true);
300 tb.checkButton(ID_STOP, false);
301 tb.checkButton(ID_PAUSE, false);
302 } else {
303 setPaused(true);
304 tb.checkButton(ID_PAUSE, true);
305 tb.checkButton(ID_PLAY, false);
306 tb.checkButton(ID_STOP, false);
307 }
308 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_CHECKED);
309 CheckMenuItem(hmenu, ID_STOP, MF_UNCHECKED);
310 break;
311 case ID_FULLSCREEN:
312 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
313 break;
george824ea27f62005-01-29 15:03:06 +0000314 case ID_RETURN:
315 // Update the speed if return pressed in speedEdit
316 if (speedEdit == GetFocus()) {
317 char speedStr[20], *stopStr;
318 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
319 double speed = strtod(speedStr, &stopStr);
320 if (speed > 0) {
321 speed = min(MAX_SPEED, speed);
322 // Update speedUpDown position
323 SendMessage(speedUpDown, UDM_SETPOS,
324 0, MAKELONG((short)(speed / 0.5), 0));
325 } else {
326 speed = getSpeed();
327 }
328 setSpeed(speed);
329 sprintf(speedStr, "%.2f", speed);
330 SetWindowText(speedEdit, speedStr);
331 }
332 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000333 }
334 break;
335
336 // Update frame's window size and add scrollbars if required
337
338 case WM_SIZE:
339 {
george82d070c692005-01-19 16:44:04 +0000340
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000341 Point old_offset = bufferToClient(Point(0, 0));
342
343 // Update the cached sizing information
344 RECT r;
345 GetClientRect(getMainHandle(), &r);
346 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
347 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
348
349 GetWindowRect(getFrameHandle(), &r);
350 window_size = Rect(r.left, r.top, r.right, r.bottom);
351 GetClientRect(getFrameHandle(), &r);
352 client_size = Rect(r.left, r.top, r.right, r.bottom);
353
354 // Determine whether scrollbars are required
355 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000356
357 // Resize the ToolBar
358 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000359
360 // Redraw if required
361 if (!old_offset.equals(bufferToClient(Point(0, 0))))
362 InvalidateRect(getFrameHandle(), 0, TRUE);
363 }
364 break;
george829e6e6cc2005-01-29 13:12:05 +0000365
366 case WM_NOTIFY:
367 switch (((NMHDR*)lParam)->code) {
368 case UDN_DELTAPOS:
369 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000370 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000371 char speedStr[20] = "\0";
372 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
373 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
374 double speed;
375
george824ea27f62005-01-29 15:03:06 +0000376 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000377 if (upDown->iDelta > 0) {
378 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
379 } else {
george824ea27f62005-01-29 15:03:06 +0000380 // It's need to round the UpDown position
381 if ((upDown->iPos * 0.5) != getSpeed()) {
382 upDown->iDelta = 0;
383 lResult = TRUE;
384 }
george829e6e6cc2005-01-29 13:12:05 +0000385 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
386 }
387 _gcvt(speed, 5, speedStr);
388 sprintf(speedStr, "%.2f", speed);
389 SetWindowText(speedEdit, speedStr);
390 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000391 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000392 }
george824ea27f62005-01-29 15:03:06 +0000393 }
george829e6e6cc2005-01-29 13:12:05 +0000394 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000395
396 case WM_CLOSE:
397 vlog.debug("WM_CLOSE %x", getMainHandle());
398 PostQuitMessage(0);
399 break;
400 }
401
402 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
403}
404
405LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
406 switch (msg) {
407
408 case WM_PAINT:
409 {
410 if (is->isSeeking()) {
411 seekMode = true;
412 return 0;
413 } else {
414 if (seekMode) {
415 seekMode = false;
416 InvalidateRect(getFrameHandle(), 0, true);
417 UpdateWindow(getFrameHandle());
418 return 0;
419 }
420 }
421
422 PAINTSTRUCT ps;
423 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
424 if (!paintDC)
425 throw SystemException("unable to BeginPaint", GetLastError());
426 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
427
428 if (!pr.is_empty()) {
429
430 if (buffer->bitmap) {
431
432 // Get device context
433 BitmapDC bitmapDC(paintDC, buffer->bitmap);
434
435 // Blit the border if required
436 Rect bufpos = bufferToClient(buffer->getRect());
437 if (!pr.enclosed_by(bufpos)) {
438 vlog.debug("draw border");
439 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
440 RECT r;
441 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
442 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
443 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
444 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
445 }
446
447 // Do the blit
448 Point buf_pos = clientToBuffer(pr.tl);
449 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
450 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
451 throw SystemException("unable to BitBlt to window", GetLastError());
452
453 } else {
454 // Blit a load of black
455 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
456 0, 0, 0, BLACKNESS))
457 throw SystemException("unable to BitBlt to blank window", GetLastError());
458 }
459 }
460 EndPaint(getFrameHandle(), &ps);
461 }
462 return 0;
463
464 case WM_VSCROLL:
465 case WM_HSCROLL:
466 {
467 Point delta;
468 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
469
470 switch (LOWORD(wParam)) {
471 case SB_PAGEUP: newpos -= 50; break;
472 case SB_PAGEDOWN: newpos += 50; break;
473 case SB_LINEUP: newpos -= 5; break;
474 case SB_LINEDOWN: newpos += 5; break;
475 case SB_THUMBTRACK:
476 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
477 default: vlog.info("received unknown scroll message");
478 };
479
480 if (msg == WM_HSCROLL)
481 setViewportOffset(Point(newpos, scrolloffset.y));
482 else
483 setViewportOffset(Point(scrolloffset.x, newpos));
484
485 SCROLLINFO si;
486 si.cbSize = sizeof(si);
487 si.fMask = SIF_POS;
488 si.nPos = newpos;
489 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
490 }
491 break;
492 }
493
494 return DefWindowProc(hwnd, msg, wParam, lParam);
495}
496
george8267cbcd02005-01-16 15:39:56 +0000497void RfbPlayer::run() {
498 long initTime = -1;
george82cfd81d12005-01-24 11:33:06 +0000499 long update_time = GetTickCount();
george8267cbcd02005-01-16 15:39:56 +0000500
george825c13c662005-01-27 14:48:23 +0000501 // Check the play button if autoplay
502 if (autoplay) {
503 tb.checkButton(ID_PLAY, true);
504 CheckMenuItem(GetMenu(getMainHandle()), ID_PLAYPAUSE, MF_CHECKED);
505 }
506
george8267cbcd02005-01-16 15:39:56 +0000507 // Process the rfb messages
508 while (fRun) {
509 try {
510 if (initTime >= 0) {
511 setPos(initTime);
512 initTime = -1;
513 }
george82cfd81d12005-01-24 11:33:06 +0000514 if ((!isSeeking()) && ((GetTickCount() - update_time) >= 250)) {
george8267cbcd02005-01-16 15:39:56 +0000515 updatePos();
george82cfd81d12005-01-24 11:33:06 +0000516 update_time = GetTickCount();
517 }
george8267cbcd02005-01-16 15:39:56 +0000518 processMsg();
519 } catch (rdr::Exception e) {
520 if (strcmp(e.str(), "[End Of File]") == 0) {
521 rewind();
522 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000523 tb.checkButton(ID_STOP, true);
524 tb.checkButton(ID_PAUSE, false);
525 tb.checkButton(ID_PLAY, false);
george8267cbcd02005-01-16 15:39:56 +0000526 continue;
527 }
528 // It's a special exception to perform backward seeking.
529 // We only rewind the stream and seek the offset
530 if (strcmp(e.str(), "[REWIND]") == 0) {
531 initTime = getSeekOffset();
532 double speed = getSpeed();
533 bool play = !isPaused();
534 rewind();
535 setSpeed(speed);
536 setPaused(!play);
537 } else {
538 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
539 return;
540 }
541 }
542 }
543}
544
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000545void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
546 bool _autoplay = false, bool _showControls = true) {
547 showControls = _showControls;
548 autoplay = _autoplay;
549 playbackSpeed = _playbackSpeed;
550 initTime = _initTime;
551}
552
553void RfbPlayer::applyOptions() {
554 if (initTime >= 0)
555 setPos(initTime);
556 setSpeed(playbackSpeed);
557 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000558}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000559
george82d070c692005-01-19 16:44:04 +0000560void RfbPlayer::createToolBar(HWND parentHwnd) {
561 RECT tRect;
562 InitCommonControls();
563
564 tb.create(ID_TOOLBAR, parentHwnd);
565 tb.addBitmap(4, IDB_TOOLBAR);
566
567 // Create the control buttons
568 tb.addButton(0, ID_PLAY);
569 tb.addButton(1, ID_PAUSE);
570 tb.addButton(2, ID_STOP);
571 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
572 tb.addButton(3, ID_FULLSCREEN);
573 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
574
575 // Create the static control for the time output
576 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
577 tb.getButtonRect(6, &tRect);
578 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
579 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
580 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
581 GetModuleHandle(0), 0);
582 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
583
584 // Create the trackbar control for the time position
585 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
586 tb.getButtonRect(8, &tRect);
587 speedTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
588 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
589 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
590 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
591 // It's need to send notify messages to toolbar parent window
592 SetParent(speedTrackBar, tb.getHandle());
593 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
594
595 // Create the label with "Speed:" caption
596 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
597 tb.getButtonRect(10, &tRect);
598 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
599 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
600 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
601
602 // Create the edit control and the spin for the speed managing
603 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
604 tb.getButtonRect(11, &tRect);
605 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
606 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
607 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
608 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
609 // It's need to send notify messages to toolbar parent window
610 SetParent(speedEdit, tb.getHandle());
611
612 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
613 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000614 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000615}
616
617void RfbPlayer::setVisible(bool visible) {
618 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
619 if (visible) {
620 // When the window becomes visible, make it active
621 SetForegroundWindow(getMainHandle());
622 SetActiveWindow(getMainHandle());
623 }
624}
625
626void RfbPlayer::setTitle(const char *title) {
627 char _title[256];
628 strcpy(_title, AppName);
629 strcat(_title, " - ");
630 strcat(_title, title);
631 SetWindowText(getMainHandle(), _title);
632}
633
634void RfbPlayer::setFrameSize(int width, int height) {
635 // Calculate and set required size for main window
636 RECT r = {0, 0, width, height};
637 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
638 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
639 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
640 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
641 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
642 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
643
644 // Enable/disable scrollbars as appropriate
645 calculateScrollBars();
646}
647
648void RfbPlayer::calculateScrollBars() {
649 // Calculate the required size of window
650 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
651 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
652 DWORD old_style;
653 RECT r;
654 SetRect(&r, 0, 0, buffer->width(), buffer->height());
655 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
656 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
657
658 // Work out whether scroll bars are required
659 do {
660 old_style = style;
661
662 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
663 style |= WS_HSCROLL;
664 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
665 }
666 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
667 style |= WS_VSCROLL;
668 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
669 }
670 } while (style != old_style);
671
672 // Tell Windows to update the window style & cached settings
673 if (style != current_style) {
674 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
675 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
676 }
677
678 // Update the scroll settings
679 SCROLLINFO si;
680 if (style & WS_VSCROLL) {
681 si.cbSize = sizeof(si);
682 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
683 si.nMin = 0;
684 si.nMax = buffer->height();
685 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
686 maxscrolloffset.y = max(0, si.nMax-si.nPage);
687 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
688 si.nPos = scrolloffset.y;
689 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
690 }
691 if (style & WS_HSCROLL) {
692 si.cbSize = sizeof(si);
693 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
694 si.nMin = 0;
695 si.nMax = buffer->width();
696 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
697 maxscrolloffset.x = max(0, si.nMax-si.nPage);
698 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
699 si.nPos = scrolloffset.x;
700 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
701 }
702}
703
704bool RfbPlayer::setViewportOffset(const Point& tl) {
705/* ***
706 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
707 max(0, min(maxscrolloffset.y, tl.y)));
708 */
709 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
710 max(0, min(tl.y, buffer->height()-client_size.height())));
711 Point delta = np.translate(scrolloffset.negate());
712 if (!np.equals(scrolloffset)) {
713 scrolloffset = np;
714 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
715 UpdateWindow(getFrameHandle());
716 return true;
717 }
718 return false;
719}
720
721void RfbPlayer::close(const char* reason) {
722 setVisible(false);
723 if (reason) {
724 vlog.info("closing - %s", reason);
725 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
726 }
727 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
728}
729
730void RfbPlayer::blankBuffer() {
731 fillRect(buffer->getRect(), 0);
732}
733
734void RfbPlayer::rewind() {
735 blankBuffer();
736 newSession(fileName);
737 skipHandshaking();
738}
739
740void RfbPlayer::serverInit() {
741 RfbProto::serverInit();
742
743 // Save the server init time for using in setPos()
744 serverInitTime = getTimeOffset() / getSpeed();
745
746 // Resize the backing buffer
747 buffer->setSize(cp.width, cp.height);
748
749 // Check on the true colour mode
750 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000751 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000752
753 // Set the session pixel format
754 buffer->setPF(cp.pf());
755
756 // If the window is not maximised then resize it
757 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
758 setFrameSize(cp.width, cp.height);
759
760 // Set the window title and show it
761 setTitle(cp.name());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000762}
763
764void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
765 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
766 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
767/* int i;
768 for (i=0;i<count;i++) {
769 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
770 }
771 // *** change to 0, 256?
772 refreshWindowPalette(first, count);
773 palette_changed = true;
774 InvalidateRect(getFrameHandle(), 0, FALSE);*/
775}
776
777void RfbPlayer::bell() {
778 if (acceptBell)
779 MessageBeep(-1);
780}
781
782void RfbPlayer::serverCutText(const char* str, int len) {
783 if (cutText != NULL)
784 delete [] cutText;
785 cutText = new char[len + 1];
786 memcpy(cutText, str, len);
787 cutText[len] = '\0';
788}
789
790void RfbPlayer::frameBufferUpdateEnd() {
791};
792
793void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
794}
795
796void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
797}
798
799
800void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
801 buffer->fillRect(r, pix);
802 invalidateBufferRect(r);
803}
804
805void RfbPlayer::imageRect(const Rect& r, void* pixels) {
806 buffer->imageRect(r, pixels);
807 invalidateBufferRect(r);
808}
809
810void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
811 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
812 invalidateBufferRect(r);
813}
814
815bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
816 Rect rect = bufferToClient(crect);
817 if (rect.intersect(client_size).is_empty()) return false;
818 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
819 InvalidateRect(getFrameHandle(), &invalid, FALSE);
820 return true;
821}
822
823void RfbPlayer::setPaused(bool paused) {
824 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000825 is->pausePlayback();
826 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000827 is->resumePlayback();
828 }
829}
830
831void RfbPlayer::setSpeed(double speed) {
832 serverInitTime = serverInitTime * getSpeed() / speed;
833 is->setSpeed(speed);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000834}
835
836double RfbPlayer::getSpeed() {
837 return is->getSpeed();
838}
839
840void RfbPlayer::setPos(long pos) {
841 is->setTimeOffset(max(pos, serverInitTime));
842}
843
844long RfbPlayer::getSeekOffset() {
845 return is->getSeekOffset();
846}
847
848bool RfbPlayer::isSeeking() {
849 return is->isSeeking();
850}
851
852bool RfbPlayer::isSeekMode() {
853 return seekMode;
854}
855
856bool RfbPlayer::isPaused() {
857 return is->isPaused();
858}
859
860long RfbPlayer::getTimeOffset() {
861 return is->getTimeOffset();
862}
863
864void RfbPlayer::updatePos() {
george823c8fbbf2005-01-24 11:09:08 +0000865 char timePos[30] = "\0";
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000866 long newPos = is->getTimeOffset() / 1000;
george823c8fbbf2005-01-24 11:09:08 +0000867 time_pos_m = newPos / 60;
868 time_pos_s = newPos % 60;
869 if (time_pos_m < 10) {
870 strcat(timePos, "0");
871 _itoa(time_pos_m, timePos+1, 10);
872 } else {
873 _itoa(time_pos_m, timePos, 10);
874 }
875 strcat(timePos, "m:");
876 if (time_pos_s < 10) {
877 strcat(timePos, "0");
878 _itoa(time_pos_s, timePos+strlen(timePos), 10);
879 } else {
880 _itoa(time_pos_s, timePos+strlen(timePos), 10);
881 }
882 strcat(timePos, "s ");
883 strcat(timePos, "(");
884 strcat(timePos, fullSessionTime);
885 strcat(timePos, ")");
886 SetWindowText(timeStatic, timePos);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000887}
888
889void RfbPlayer::skipHandshaking() {
890 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
891 is->skip(skipBytes);
892 state_ = RFBSTATE_NORMAL;
893}
894
895void programInfo() {
896 win32::FileVersionInfo inf;
897 _tprintf(_T("%s - %s, Version %s\n"),
898 inf.getVerString(_T("ProductName")),
899 inf.getVerString(_T("FileDescription")),
900 inf.getVerString(_T("FileVersion")));
901 printf("%s\n", buildTime);
902 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
903}
904
905void programUsage() {
906 printf("usage: rfbplayer <options> <filename>\n");
907 printf("Command-line options:\n");
908 printf(" -help - Provide usage information.\n");
909 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
910 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
911 printf(" -pos <ms> - Sets initial time position in the session file,\n");
912 printf(" in milliseconds. Default: 0.\n");
913 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
914 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
915 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
916}
917
918double playbackSpeed = 1.0;
919long initTime = -1;
920bool autoplay = false;
921bool showControls = true;
922char *fileName;
923bool console = false;
924bool wrong_param = false;
925bool print_usage = false;
926bool acceptBell = false;
927
928bool processParams(int argc, char* argv[]) {
929 for (int i = 1; i < argc; i++) {
930 if ((strcasecmp(argv[i], "-help") == 0) ||
931 (strcasecmp(argv[i], "--help") == 0) ||
932 (strcasecmp(argv[i], "/help") == 0) ||
933 (strcasecmp(argv[i], "-h") == 0) ||
934 (strcasecmp(argv[i], "/h") == 0) ||
935 (strcasecmp(argv[i], "/?") == 0)) {
936 print_usage = true;
937 return true;
938 }
939
940 if ((strcasecmp(argv[i], "-speed") == 0) ||
941 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
942 playbackSpeed = atof(argv[++i]);
943 if (playbackSpeed <= 0) {
944 return false;
945 }
946 continue;
947 }
948
949 if ((strcasecmp(argv[i], "-pos") == 0) ||
950 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
951 initTime = atol(argv[++i]);
952 if (initTime <= 0)
953 return false;
954 continue;
955 }
956
957 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
958 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
959 i++;
960 if (strcasecmp(argv[i], "yes") == 0) {
961 autoplay = true;
962 continue;
963 }
964 if (strcasecmp(argv[i], "no") == 0) {
965 autoplay = false;
966 continue;
967 }
968 return false;
969 }
970
971 if ((strcasecmp(argv[i], "-controls") == 0) ||
972 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
973 i++;
974 if (strcasecmp(argv[i], "yes") == 0) {
975 showControls = true;
976 continue;
977 }
978 if (strcasecmp(argv[i], "no") == 0) {
979 showControls = false;
980 continue;
981 }
982 return false;
983 }
984
985 if ((strcasecmp(argv[i], "-bell") == 0) ||
986 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
987 i++;
988 if (strcasecmp(argv[i], "yes") == 0) {
989 acceptBell = true;
990 continue;
991 }
992 if (strcasecmp(argv[i], "no") == 0) {
993 acceptBell = false;
994 continue;
995 }
996 return false;
997 }
998
999 if (i != argc - 1)
1000 return false;
1001 }
1002
1003 fileName = strDup(argv[argc-1]);
1004 return true;
1005}
1006
1007//
1008// -=- WinMain
1009//
1010
1011int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1012
1013 // - Process the command-line
1014
1015 int argc = __argc;
1016 char** argv = __argv;
1017 if (argc > 1) {
1018 wrong_param = !processParams(argc, argv);
1019 console = print_usage | wrong_param;
1020 } else {
1021 console = true;
1022 }
1023
1024 if (console) {
1025 AllocConsole();
1026 freopen("CONOUT$","wb",stdout);
1027
1028 programInfo();
1029 if (wrong_param)
1030 printf("Wrong a command line.\n");
1031 else
1032 programUsage();
1033
1034 printf("\nPress Enter/Return key to continue\n");
1035 char c = getch();
1036 FreeConsole();
1037
1038 return 0;
george8267cbcd02005-01-16 15:39:56 +00001039 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001040
1041 // Create the player and the thread which reading the rfb data
1042 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001043 try {
1044 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
1045 showControls, acceptBell);
george8210313102005-01-17 13:11:40 +00001046 if (autoplay) player->start();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001047 } catch (rdr::Exception e) {
1048 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1049 delete player;
1050 return 0;
1051 }
1052
1053 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001054 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001055 MSG msg;
1056 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001057 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1058 TranslateMessage(&msg);
1059 DispatchMessage(&msg);
1060 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001061 }
1062
1063 // Wait while the thread destroying and then destroy the player
1064 try{
george8267cbcd02005-01-16 15:39:56 +00001065 while (player->getState() == ThreadStarted) {}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001066 if (player) delete player;
1067 } catch (rdr::Exception e) {
1068 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1069 }
1070
1071 return 0;
1072};