blob: 169d716143cf2953cfcd0710f3aa19f9537a9c2b [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);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000081
82 // Resume playback (It's need to quit from FbsInputStream::waitWhilePaused())
83 _this->setPaused(false);
84 SetWindowLong(hwnd, GWL_USERDATA, 0);
85 }
86 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
87 if (!_this) {
88 vlog.info("null _this in %x, message %u", hwnd, msg);
89 return DefWindowProc(hwnd, msg, wParam, lParam);
90 }
91
92 try {
93 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
94 } catch (rdr::Exception& e) {
95 vlog.error("untrapped: %s", e.str());
96 }
97
98 return result;
99};
100
101RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
102 WNDCLASS wndClass;
103 wndClass.style = 0;
104 wndClass.lpfnWndProc = RfbPlayerProc;
105 wndClass.cbClsExtra = 0;
106 wndClass.cbWndExtra = 0;
107 wndClass.hInstance = instance = GetModuleHandle(0);
108 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000109 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000110 if (!wndClass.hIcon)
111 printf("unable to load icon:%ld", GetLastError());
112 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
113 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000114 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000115 wndClass.lpszClassName = _T("RfbPlayerClass");
116 classAtom = RegisterClass(&wndClass);
117 if (!classAtom) {
118 throw rdr::SystemException("unable to register RfbPlayer window class",
119 GetLastError());
120 }
121}
122
123RfbPlayerClass::~RfbPlayerClass() {
124 if (classAtom) {
125 UnregisterClass((const TCHAR*)classAtom, instance);
126 }
127}
128
129RfbPlayerClass baseClass;
130
131//
132// -=- RfbFrameClass
133
134//
135// Window class used to displaying the rfb data
136//
137
138class RfbFrameClass {
139public:
140 RfbFrameClass();
141 ~RfbFrameClass();
142 ATOM classAtom;
143 HINSTANCE instance;
144};
145
146LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
147 LRESULT result;
148
149 if (msg == WM_CREATE)
150 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
151 else if (msg == WM_DESTROY)
152 SetWindowLong(hwnd, GWL_USERDATA, 0);
153 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
154 if (!_this) {
155 vlog.info("null _this in %x, message %u", hwnd, msg);
156 return DefWindowProc(hwnd, msg, wParam, lParam);
157 }
158
159 try {
160 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
161 } catch (rdr::Exception& e) {
162 vlog.error("untrapped: %s", e.str());
163 }
164
165 return result;
166}
167
168RfbFrameClass::RfbFrameClass() : classAtom(0) {
169 WNDCLASS wndClass;
170 wndClass.style = 0;
171 wndClass.lpfnWndProc = FrameProc;
172 wndClass.cbClsExtra = 0;
173 wndClass.cbWndExtra = 0;
174 wndClass.hInstance = instance = GetModuleHandle(0);
175 wndClass.hIcon = 0;
176 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
177 wndClass.hbrBackground = 0;
178 wndClass.lpszMenuName = 0;
179 wndClass.lpszClassName = _T("RfbPlayerClass1");
180 classAtom = RegisterClass(&wndClass);
181 if (!classAtom) {
182 throw rdr::SystemException("unable to register RfbPlayer window class",
183 GetLastError());
184 }
185}
186
187RfbFrameClass::~RfbFrameClass() {
188 if (classAtom) {
189 UnregisterClass((const TCHAR*)classAtom, instance);
190 }
191}
192
193RfbFrameClass frameClass;
194
195//
196// -=- RfbPlayer instance implementation
197//
198
199RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
200 bool _autoplay = false, bool _showControls = true,
201 bool _acceptBell = false)
202: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
203 autoplay(_autoplay), showControls(_showControls), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000204 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d070c692005-01-19 16:44:04 +0000205 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), speedTrackBar(0),
george82ce8dc3a2005-01-31 13:06:54 +0000206 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000207
208 if (showControls)
george82d070c692005-01-19 16:44:04 +0000209 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000210 else
211 CTRL_BAR_HEIGHT = 0;
212
george823c8fbbf2005-01-24 11:09:08 +0000213 // Reset the full session time
214 strcpy(fullSessionTime, "00m:00s");
215
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000216 // Create the main window
217 const TCHAR* name = _T("RfbPlayer");
218 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000219 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000220 if (!mainHwnd) {
221 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
222 }
223 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
224
225 // Create the backing buffer
226 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000227 setVisible(true);
george8217e92cb2005-01-31 16:01:02 +0000228
229 // Open the session file
230 if (fileName) {
231 openSessionFile(fileName);
232 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000233}
234
235RfbPlayer::~RfbPlayer() {
236 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000237 if (rfbReader) {
238 rfbReader->stop();
239 delete rfbReader->join();
240 rfbReader = 0;
241 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000242 if (mainHwnd) {
243 setVisible(false);
244 DestroyWindow(mainHwnd);
245 mainHwnd = 0;
246 }
247 delete buffer;
248 delete cutText;
249 vlog.debug("~RfbPlayer done");
250}
251
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000252LRESULT
253RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
george825c13c662005-01-27 14:48:23 +0000254 static HMENU hmenu; // handle to main menu
255
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000256 switch (msg) {
257
258 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000259
260 case WM_CREATE:
261 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000262 // Create the frame window
263 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
264 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
265 hwnd, 0, frameClass.instance, this);
266
george82d070c692005-01-19 16:44:04 +0000267 createToolBar(hwnd);
268
george825c13c662005-01-27 14:48:23 +0000269 hmenu = GetMenu(hwnd);
270
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000271 return 0;
272 }
273
george827214b822004-12-12 07:02:51 +0000274 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000275
276 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000277 switch (LOWORD(wParam)) {
278 case ID_PLAY:
279 setPaused(false);
280 tb.checkButton(ID_PLAY, true);
281 tb.checkButton(ID_STOP, false);
282 tb.checkButton(ID_PAUSE, false);
283 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_CHECKED);
284 CheckMenuItem(hmenu, ID_STOP, MF_UNCHECKED);
285 break;
286 case ID_PAUSE:
287 setPaused(true);
288 tb.checkButton(ID_PAUSE, true);
289 tb.checkButton(ID_PLAY, false);
290 tb.checkButton(ID_STOP, false);
291 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_CHECKED);
292 CheckMenuItem(hmenu, ID_STOP, MF_UNCHECKED);
293 break;
294 case ID_STOP:
295 if (getTimeOffset() != 0) {
george825c13c662005-01-27 14:48:23 +0000296 setPos(0);
george8223e08562005-01-31 15:16:42 +0000297 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000298 }
299 tb.checkButton(ID_STOP, true);
300 tb.checkButton(ID_PLAY, false);
301 tb.checkButton(ID_PAUSE, false);
302 CheckMenuItem(hmenu, ID_STOP, MF_CHECKED);
303 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_UNCHECKED);
304 break;
305 case ID_PLAYPAUSE:
306 if (isPaused()) {
307 setPaused(false);
308 tb.checkButton(ID_PLAY, true);
309 tb.checkButton(ID_STOP, false);
310 tb.checkButton(ID_PAUSE, false);
311 } else {
312 setPaused(true);
313 tb.checkButton(ID_PAUSE, true);
314 tb.checkButton(ID_PLAY, false);
315 tb.checkButton(ID_STOP, false);
316 }
317 CheckMenuItem(hmenu, ID_PLAYPAUSE, MF_CHECKED);
318 CheckMenuItem(hmenu, ID_STOP, MF_UNCHECKED);
319 break;
320 case ID_FULLSCREEN:
321 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
322 break;
george824ea27f62005-01-29 15:03:06 +0000323 case ID_RETURN:
324 // Update the speed if return pressed in speedEdit
325 if (speedEdit == GetFocus()) {
326 char speedStr[20], *stopStr;
327 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
328 double speed = strtod(speedStr, &stopStr);
329 if (speed > 0) {
330 speed = min(MAX_SPEED, speed);
331 // Update speedUpDown position
332 SendMessage(speedUpDown, UDM_SETPOS,
333 0, MAKELONG((short)(speed / 0.5), 0));
334 } else {
335 speed = getSpeed();
336 }
337 setSpeed(speed);
338 sprintf(speedStr, "%.2f", speed);
339 SetWindowText(speedEdit, speedStr);
340 }
341 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000342 }
343 break;
344
345 // Update frame's window size and add scrollbars if required
346
347 case WM_SIZE:
348 {
george82d070c692005-01-19 16:44:04 +0000349
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000350 Point old_offset = bufferToClient(Point(0, 0));
351
352 // Update the cached sizing information
353 RECT r;
354 GetClientRect(getMainHandle(), &r);
355 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
356 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
357
358 GetWindowRect(getFrameHandle(), &r);
359 window_size = Rect(r.left, r.top, r.right, r.bottom);
360 GetClientRect(getFrameHandle(), &r);
361 client_size = Rect(r.left, r.top, r.right, r.bottom);
362
363 // Determine whether scrollbars are required
364 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000365
366 // Resize the ToolBar
367 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000368
369 // Redraw if required
370 if (!old_offset.equals(bufferToClient(Point(0, 0))))
371 InvalidateRect(getFrameHandle(), 0, TRUE);
372 }
373 break;
george829e6e6cc2005-01-29 13:12:05 +0000374
375 case WM_NOTIFY:
376 switch (((NMHDR*)lParam)->code) {
377 case UDN_DELTAPOS:
378 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000379 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000380 char speedStr[20] = "\0";
381 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
382 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
383 double speed;
384
george824ea27f62005-01-29 15:03:06 +0000385 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000386 if (upDown->iDelta > 0) {
387 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
388 } else {
george824ea27f62005-01-29 15:03:06 +0000389 // It's need to round the UpDown position
390 if ((upDown->iPos * 0.5) != getSpeed()) {
391 upDown->iDelta = 0;
392 lResult = TRUE;
393 }
george829e6e6cc2005-01-29 13:12:05 +0000394 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
395 }
396 _gcvt(speed, 5, speedStr);
397 sprintf(speedStr, "%.2f", speed);
398 SetWindowText(speedEdit, speedStr);
399 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000400 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000401 }
george824ea27f62005-01-29 15:03:06 +0000402 }
george829e6e6cc2005-01-29 13:12:05 +0000403 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000404
405 case WM_CLOSE:
406 vlog.debug("WM_CLOSE %x", getMainHandle());
407 PostQuitMessage(0);
408 break;
409 }
410
411 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
412}
413
414LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
415 switch (msg) {
416
417 case WM_PAINT:
418 {
419 if (is->isSeeking()) {
420 seekMode = true;
421 return 0;
422 } else {
423 if (seekMode) {
424 seekMode = false;
425 InvalidateRect(getFrameHandle(), 0, true);
426 UpdateWindow(getFrameHandle());
427 return 0;
428 }
429 }
430
431 PAINTSTRUCT ps;
432 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
433 if (!paintDC)
434 throw SystemException("unable to BeginPaint", GetLastError());
435 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
436
437 if (!pr.is_empty()) {
438
439 if (buffer->bitmap) {
440
441 // Get device context
442 BitmapDC bitmapDC(paintDC, buffer->bitmap);
443
444 // Blit the border if required
445 Rect bufpos = bufferToClient(buffer->getRect());
446 if (!pr.enclosed_by(bufpos)) {
447 vlog.debug("draw border");
448 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
449 RECT r;
450 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
451 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
452 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
453 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
454 }
455
456 // Do the blit
457 Point buf_pos = clientToBuffer(pr.tl);
458 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
459 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
460 throw SystemException("unable to BitBlt to window", GetLastError());
461
462 } else {
463 // Blit a load of black
464 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
465 0, 0, 0, BLACKNESS))
466 throw SystemException("unable to BitBlt to blank window", GetLastError());
467 }
468 }
469 EndPaint(getFrameHandle(), &ps);
470 }
471 return 0;
472
473 case WM_VSCROLL:
474 case WM_HSCROLL:
475 {
476 Point delta;
477 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
478
479 switch (LOWORD(wParam)) {
480 case SB_PAGEUP: newpos -= 50; break;
481 case SB_PAGEDOWN: newpos += 50; break;
482 case SB_LINEUP: newpos -= 5; break;
483 case SB_LINEDOWN: newpos += 5; break;
484 case SB_THUMBTRACK:
485 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
486 default: vlog.info("received unknown scroll message");
487 };
488
489 if (msg == WM_HSCROLL)
490 setViewportOffset(Point(newpos, scrolloffset.y));
491 else
492 setViewportOffset(Point(scrolloffset.x, newpos));
493
494 SCROLLINFO si;
495 si.cbSize = sizeof(si);
496 si.fMask = SIF_POS;
497 si.nPos = newpos;
498 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
499 }
500 break;
501 }
502
503 return DefWindowProc(hwnd, msg, wParam, lParam);
504}
505
506void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
507 bool _autoplay = false, bool _showControls = true) {
508 showControls = _showControls;
509 autoplay = _autoplay;
510 playbackSpeed = _playbackSpeed;
511 initTime = _initTime;
512}
513
514void RfbPlayer::applyOptions() {
515 if (initTime >= 0)
516 setPos(initTime);
517 setSpeed(playbackSpeed);
518 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000519}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000520
george82d070c692005-01-19 16:44:04 +0000521void RfbPlayer::createToolBar(HWND parentHwnd) {
522 RECT tRect;
523 InitCommonControls();
524
525 tb.create(ID_TOOLBAR, parentHwnd);
526 tb.addBitmap(4, IDB_TOOLBAR);
527
528 // Create the control buttons
529 tb.addButton(0, ID_PLAY);
530 tb.addButton(1, ID_PAUSE);
531 tb.addButton(2, ID_STOP);
532 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
533 tb.addButton(3, ID_FULLSCREEN);
534 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
535
536 // Create the static control for the time output
537 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
538 tb.getButtonRect(6, &tRect);
539 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
540 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
541 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
542 GetModuleHandle(0), 0);
543 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
544
545 // Create the trackbar control for the time position
546 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
547 tb.getButtonRect(8, &tRect);
548 speedTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
549 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
550 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
551 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
552 // It's need to send notify messages to toolbar parent window
553 SetParent(speedTrackBar, tb.getHandle());
554 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
555
556 // Create the label with "Speed:" caption
557 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
558 tb.getButtonRect(10, &tRect);
559 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
560 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
561 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
562
563 // Create the edit control and the spin for the speed managing
564 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
565 tb.getButtonRect(11, &tRect);
566 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
567 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
568 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
569 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
570 // It's need to send notify messages to toolbar parent window
571 SetParent(speedEdit, tb.getHandle());
572
573 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
574 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000575 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000576}
577
578void RfbPlayer::setVisible(bool visible) {
579 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
580 if (visible) {
581 // When the window becomes visible, make it active
582 SetForegroundWindow(getMainHandle());
583 SetActiveWindow(getMainHandle());
584 }
585}
586
587void RfbPlayer::setTitle(const char *title) {
588 char _title[256];
589 strcpy(_title, AppName);
590 strcat(_title, " - ");
591 strcat(_title, title);
592 SetWindowText(getMainHandle(), _title);
593}
594
595void RfbPlayer::setFrameSize(int width, int height) {
596 // Calculate and set required size for main window
597 RECT r = {0, 0, width, height};
598 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
599 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
600 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
601 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
602 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
603 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
604
605 // Enable/disable scrollbars as appropriate
606 calculateScrollBars();
607}
608
609void RfbPlayer::calculateScrollBars() {
610 // Calculate the required size of window
611 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
612 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
613 DWORD old_style;
614 RECT r;
615 SetRect(&r, 0, 0, buffer->width(), buffer->height());
616 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
617 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
618
619 // Work out whether scroll bars are required
620 do {
621 old_style = style;
622
623 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
624 style |= WS_HSCROLL;
625 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
626 }
627 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
628 style |= WS_VSCROLL;
629 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
630 }
631 } while (style != old_style);
632
633 // Tell Windows to update the window style & cached settings
634 if (style != current_style) {
635 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
636 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
637 }
638
639 // Update the scroll settings
640 SCROLLINFO si;
641 if (style & WS_VSCROLL) {
642 si.cbSize = sizeof(si);
643 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
644 si.nMin = 0;
645 si.nMax = buffer->height();
646 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
647 maxscrolloffset.y = max(0, si.nMax-si.nPage);
648 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
649 si.nPos = scrolloffset.y;
650 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
651 }
652 if (style & WS_HSCROLL) {
653 si.cbSize = sizeof(si);
654 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
655 si.nMin = 0;
656 si.nMax = buffer->width();
657 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
658 maxscrolloffset.x = max(0, si.nMax-si.nPage);
659 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
660 si.nPos = scrolloffset.x;
661 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
662 }
663}
664
665bool RfbPlayer::setViewportOffset(const Point& tl) {
666/* ***
667 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
668 max(0, min(maxscrolloffset.y, tl.y)));
669 */
670 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
671 max(0, min(tl.y, buffer->height()-client_size.height())));
672 Point delta = np.translate(scrolloffset.negate());
673 if (!np.equals(scrolloffset)) {
674 scrolloffset = np;
675 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
676 UpdateWindow(getFrameHandle());
677 return true;
678 }
679 return false;
680}
681
682void RfbPlayer::close(const char* reason) {
683 setVisible(false);
684 if (reason) {
685 vlog.info("closing - %s", reason);
686 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
687 }
688 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
689}
690
691void RfbPlayer::blankBuffer() {
692 fillRect(buffer->getRect(), 0);
693}
694
695void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000696 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000697 blankBuffer();
698 newSession(fileName);
699 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000700 setSpeed(playbackSpeed);
701 setPaused(paused);
702}
703
704void RfbPlayer::processMsg() {
705 static long update_time = GetTickCount();
706 try {
707 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)) {
708 // Update pos in the toolbar 4 times in 1 second
709 updatePos();
710 update_time = GetTickCount();
711 }
712 RfbProto::processMsg();
713 } catch (rdr::Exception e) {
714 if (strcmp(e.str(), "[End Of File]") == 0) {
715 rewind();
716 setPaused(true);
717 tb.checkButton(ID_STOP, true);
718 tb.checkButton(ID_PAUSE, false);
719 tb.checkButton(ID_PLAY, false);
720 return;
721 }
722 // It's a special exception to perform backward seeking.
723 // We only rewind the stream and seek the offset
724 if (strcmp(e.str(), "[REWIND]") == 0) {
725 long initTime = getSeekOffset();
726 rewind();
727 setPos(initTime);
728 } else {
729 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
730 return;
731 }
732 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000733}
734
735void RfbPlayer::serverInit() {
736 RfbProto::serverInit();
737
738 // Save the server init time for using in setPos()
739 serverInitTime = getTimeOffset() / getSpeed();
740
741 // Resize the backing buffer
742 buffer->setSize(cp.width, cp.height);
743
744 // Check on the true colour mode
745 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000746 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000747
748 // Set the session pixel format
749 buffer->setPF(cp.pf());
750
751 // If the window is not maximised then resize it
752 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
753 setFrameSize(cp.width, cp.height);
754
755 // Set the window title and show it
756 setTitle(cp.name());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000757}
758
759void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
760 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
761 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
762/* int i;
763 for (i=0;i<count;i++) {
764 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
765 }
766 // *** change to 0, 256?
767 refreshWindowPalette(first, count);
768 palette_changed = true;
769 InvalidateRect(getFrameHandle(), 0, FALSE);*/
770}
771
772void RfbPlayer::bell() {
773 if (acceptBell)
774 MessageBeep(-1);
775}
776
777void RfbPlayer::serverCutText(const char* str, int len) {
778 if (cutText != NULL)
779 delete [] cutText;
780 cutText = new char[len + 1];
781 memcpy(cutText, str, len);
782 cutText[len] = '\0';
783}
784
785void RfbPlayer::frameBufferUpdateEnd() {
786};
787
788void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
789}
790
791void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
792}
793
794
795void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
796 buffer->fillRect(r, pix);
797 invalidateBufferRect(r);
798}
799
800void RfbPlayer::imageRect(const Rect& r, void* pixels) {
801 buffer->imageRect(r, pixels);
802 invalidateBufferRect(r);
803}
804
805void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
806 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
807 invalidateBufferRect(r);
808}
809
810bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
811 Rect rect = bufferToClient(crect);
812 if (rect.intersect(client_size).is_empty()) return false;
813 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
814 InvalidateRect(getFrameHandle(), &invalid, FALSE);
815 return true;
816}
817
george8217e92cb2005-01-31 16:01:02 +0000818void RfbPlayer::openSessionFile(char *_fileName) {
819 fileName = strDup(_fileName);
820
821 // Close the previous reading thread
822 if (rfbReader) {
823 rfbReader->stop();
824 delete rfbReader->join();
825 }
826 blankBuffer();
827 newSession(fileName);
828 setSpeed(playbackSpeed);
829 rfbReader = new rfbSessionReader(this);
830 rfbReader->start();
831}
832
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000833void RfbPlayer::setPaused(bool paused) {
834 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000835 is->pausePlayback();
836 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000837 is->resumePlayback();
838 }
839}
840
841void RfbPlayer::setSpeed(double speed) {
842 serverInitTime = serverInitTime * getSpeed() / speed;
843 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000844 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000845}
846
847double RfbPlayer::getSpeed() {
848 return is->getSpeed();
849}
850
851void RfbPlayer::setPos(long pos) {
852 is->setTimeOffset(max(pos, serverInitTime));
853}
854
855long RfbPlayer::getSeekOffset() {
856 return is->getSeekOffset();
857}
858
859bool RfbPlayer::isSeeking() {
860 return is->isSeeking();
861}
862
863bool RfbPlayer::isSeekMode() {
864 return seekMode;
865}
866
867bool RfbPlayer::isPaused() {
868 return is->isPaused();
869}
870
871long RfbPlayer::getTimeOffset() {
872 return is->getTimeOffset();
873}
874
875void RfbPlayer::updatePos() {
george823c8fbbf2005-01-24 11:09:08 +0000876 char timePos[30] = "\0";
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000877 long newPos = is->getTimeOffset() / 1000;
george823c8fbbf2005-01-24 11:09:08 +0000878 time_pos_m = newPos / 60;
879 time_pos_s = newPos % 60;
880 if (time_pos_m < 10) {
881 strcat(timePos, "0");
882 _itoa(time_pos_m, timePos+1, 10);
883 } else {
884 _itoa(time_pos_m, timePos, 10);
885 }
886 strcat(timePos, "m:");
887 if (time_pos_s < 10) {
888 strcat(timePos, "0");
889 _itoa(time_pos_s, timePos+strlen(timePos), 10);
890 } else {
891 _itoa(time_pos_s, timePos+strlen(timePos), 10);
892 }
893 strcat(timePos, "s ");
894 strcat(timePos, "(");
895 strcat(timePos, fullSessionTime);
896 strcat(timePos, ")");
897 SetWindowText(timeStatic, timePos);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000898}
899
900void RfbPlayer::skipHandshaking() {
901 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
902 is->skip(skipBytes);
903 state_ = RFBSTATE_NORMAL;
904}
905
906void programInfo() {
907 win32::FileVersionInfo inf;
908 _tprintf(_T("%s - %s, Version %s\n"),
909 inf.getVerString(_T("ProductName")),
910 inf.getVerString(_T("FileDescription")),
911 inf.getVerString(_T("FileVersion")));
912 printf("%s\n", buildTime);
913 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
914}
915
916void programUsage() {
917 printf("usage: rfbplayer <options> <filename>\n");
918 printf("Command-line options:\n");
919 printf(" -help - Provide usage information.\n");
920 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
921 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
922 printf(" -pos <ms> - Sets initial time position in the session file,\n");
923 printf(" in milliseconds. Default: 0.\n");
924 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
925 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
926 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
927}
928
929double playbackSpeed = 1.0;
930long initTime = -1;
931bool autoplay = false;
932bool showControls = true;
933char *fileName;
934bool console = false;
935bool wrong_param = false;
936bool print_usage = false;
937bool acceptBell = false;
938
939bool processParams(int argc, char* argv[]) {
940 for (int i = 1; i < argc; i++) {
941 if ((strcasecmp(argv[i], "-help") == 0) ||
942 (strcasecmp(argv[i], "--help") == 0) ||
943 (strcasecmp(argv[i], "/help") == 0) ||
944 (strcasecmp(argv[i], "-h") == 0) ||
945 (strcasecmp(argv[i], "/h") == 0) ||
946 (strcasecmp(argv[i], "/?") == 0)) {
947 print_usage = true;
948 return true;
949 }
950
951 if ((strcasecmp(argv[i], "-speed") == 0) ||
952 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
953 playbackSpeed = atof(argv[++i]);
954 if (playbackSpeed <= 0) {
955 return false;
956 }
957 continue;
958 }
959
960 if ((strcasecmp(argv[i], "-pos") == 0) ||
961 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
962 initTime = atol(argv[++i]);
963 if (initTime <= 0)
964 return false;
965 continue;
966 }
967
968 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
969 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
970 i++;
971 if (strcasecmp(argv[i], "yes") == 0) {
972 autoplay = true;
973 continue;
974 }
975 if (strcasecmp(argv[i], "no") == 0) {
976 autoplay = false;
977 continue;
978 }
979 return false;
980 }
981
982 if ((strcasecmp(argv[i], "-controls") == 0) ||
983 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
984 i++;
985 if (strcasecmp(argv[i], "yes") == 0) {
986 showControls = true;
987 continue;
988 }
989 if (strcasecmp(argv[i], "no") == 0) {
990 showControls = false;
991 continue;
992 }
993 return false;
994 }
995
996 if ((strcasecmp(argv[i], "-bell") == 0) ||
997 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
998 i++;
999 if (strcasecmp(argv[i], "yes") == 0) {
1000 acceptBell = true;
1001 continue;
1002 }
1003 if (strcasecmp(argv[i], "no") == 0) {
1004 acceptBell = false;
1005 continue;
1006 }
1007 return false;
1008 }
1009
1010 if (i != argc - 1)
1011 return false;
1012 }
1013
1014 fileName = strDup(argv[argc-1]);
1015 return true;
1016}
1017
1018//
1019// -=- WinMain
1020//
1021
1022int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1023
1024 // - Process the command-line
1025
1026 int argc = __argc;
1027 char** argv = __argv;
1028 if (argc > 1) {
1029 wrong_param = !processParams(argc, argv);
1030 console = print_usage | wrong_param;
1031 } else {
1032 console = true;
1033 }
1034
1035 if (console) {
1036 AllocConsole();
1037 freopen("CONOUT$","wb",stdout);
1038
1039 programInfo();
1040 if (wrong_param)
1041 printf("Wrong a command line.\n");
1042 else
1043 programUsage();
1044
1045 printf("\nPress Enter/Return key to continue\n");
1046 char c = getch();
1047 FreeConsole();
1048
1049 return 0;
george8267cbcd02005-01-16 15:39:56 +00001050 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001051
1052 // Create the player and the thread which reading the rfb data
1053 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001054 try {
1055 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
1056 showControls, acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001057 } catch (rdr::Exception e) {
1058 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1059 delete player;
1060 return 0;
1061 }
1062
1063 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001064 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001065 MSG msg;
1066 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001067 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1068 TranslateMessage(&msg);
1069 DispatchMessage(&msg);
1070 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001071 }
1072
1073 // Wait while the thread destroying and then destroy the player
1074 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001075 if (player) delete player;
1076 } catch (rdr::Exception e) {
1077 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1078 }
1079
1080 return 0;
1081};