blob: 3e739c6653224a707dbf5a04013ad5dcbd743978 [file] [log] [blame]
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001/* Copyright (C) 2004 TightVNC Team. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- RFB Player for Win32
20
21#include <conio.h>
22
23#include <rfb/LogWriter.h>
24#include <rfb/Exception.h>
25#include <rfb/Threading.h>
26
27#include <rfb_win32/Win32Util.h>
28#include <rfb_win32/WMShatter.h>
29
30#include <rfbplayer/rfbplayer.h>
31#include <rfbplayer/utils.h>
32#include <rfbplayer/resource.h>
33
34using namespace rfb;
35using namespace rfb::win32;
36
37// -=- Variables & consts
38
39static LogWriter vlog("RfbPlayer");
40
41TStr rfb::win32::AppName("RfbPlayer");
42extern const char* buildTime;
43
44// -=- RfbPlayer's defines
45
46#define strcasecmp _stricmp
george824ea27f62005-01-29 15:03:06 +000047#define MAX_SPEED 10
george82d4d69e62005-02-05 09:23:18 +000048#define MAX_POS_TRACKBAR_RANGE 50
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000049
george82d070c692005-01-19 16:44:04 +000050#define ID_TOOLBAR 500
51#define ID_PLAY 510
52#define ID_PAUSE 520
53#define ID_TIME_STATIC 530
54#define ID_SPEED_STATIC 540
55#define ID_SPEED_EDIT 550
56#define ID_POS_TRACKBAR 560
57#define ID_SPEED_UPDOWN 570
58
59
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000060//
61// -=- RfbPlayerClass
62
63//
64// Window class used as the basis for RfbPlayer instance
65//
66
67class RfbPlayerClass {
68public:
69 RfbPlayerClass();
70 ~RfbPlayerClass();
71 ATOM classAtom;
72 HINSTANCE instance;
73};
74
75LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
76 LRESULT result;
77
78 if (msg == WM_CREATE)
79 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
80 else if (msg == WM_DESTROY) {
81 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000082
83 // Resume playback (It's need to quit from FbsInputStream::waitWhilePaused())
84 _this->setPaused(false);
85 SetWindowLong(hwnd, GWL_USERDATA, 0);
86 }
87 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
88 if (!_this) {
89 vlog.info("null _this in %x, message %u", hwnd, msg);
90 return DefWindowProc(hwnd, msg, wParam, lParam);
91 }
92
93 try {
94 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
95 } catch (rdr::Exception& e) {
96 vlog.error("untrapped: %s", e.str());
97 }
98
99 return result;
100};
101
102RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
103 WNDCLASS wndClass;
104 wndClass.style = 0;
105 wndClass.lpfnWndProc = RfbPlayerProc;
106 wndClass.cbClsExtra = 0;
107 wndClass.cbWndExtra = 0;
108 wndClass.hInstance = instance = GetModuleHandle(0);
109 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000110 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000111 if (!wndClass.hIcon)
112 printf("unable to load icon:%ld", GetLastError());
113 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
114 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000115 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000116 wndClass.lpszClassName = _T("RfbPlayerClass");
117 classAtom = RegisterClass(&wndClass);
118 if (!classAtom) {
119 throw rdr::SystemException("unable to register RfbPlayer window class",
120 GetLastError());
121 }
122}
123
124RfbPlayerClass::~RfbPlayerClass() {
125 if (classAtom) {
126 UnregisterClass((const TCHAR*)classAtom, instance);
127 }
128}
129
130RfbPlayerClass baseClass;
131
132//
133// -=- RfbFrameClass
134
135//
136// Window class used to displaying the rfb data
137//
138
139class RfbFrameClass {
140public:
141 RfbFrameClass();
142 ~RfbFrameClass();
143 ATOM classAtom;
144 HINSTANCE instance;
145};
146
147LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
148 LRESULT result;
149
150 if (msg == WM_CREATE)
151 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
152 else if (msg == WM_DESTROY)
153 SetWindowLong(hwnd, GWL_USERDATA, 0);
154 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
155 if (!_this) {
156 vlog.info("null _this in %x, message %u", hwnd, msg);
157 return DefWindowProc(hwnd, msg, wParam, lParam);
158 }
159
160 try {
161 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
162 } catch (rdr::Exception& e) {
163 vlog.error("untrapped: %s", e.str());
164 }
165
166 return result;
167}
168
169RfbFrameClass::RfbFrameClass() : classAtom(0) {
170 WNDCLASS wndClass;
171 wndClass.style = 0;
172 wndClass.lpfnWndProc = FrameProc;
173 wndClass.cbClsExtra = 0;
174 wndClass.cbWndExtra = 0;
175 wndClass.hInstance = instance = GetModuleHandle(0);
176 wndClass.hIcon = 0;
177 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
178 wndClass.hbrBackground = 0;
179 wndClass.lpszMenuName = 0;
180 wndClass.lpszClassName = _T("RfbPlayerClass1");
181 classAtom = RegisterClass(&wndClass);
182 if (!classAtom) {
183 throw rdr::SystemException("unable to register RfbPlayer window class",
184 GetLastError());
185 }
186}
187
188RfbFrameClass::~RfbFrameClass() {
189 if (classAtom) {
190 UnregisterClass((const TCHAR*)classAtom, instance);
191 }
192}
193
194RfbFrameClass frameClass;
195
196//
197// -=- RfbPlayer instance implementation
198//
199
200RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
201 bool _autoplay = false, bool _showControls = true,
202 bool _acceptBell = false)
203: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
204 autoplay(_autoplay), showControls(_showControls), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000205 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000206 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george82ce8dc3a2005-01-31 13:06:54 +0000207 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0) {
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);
george8217e92cb2005-01-31 16:01:02 +0000229
230 // Open the session file
231 if (fileName) {
232 openSessionFile(fileName);
233 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000234}
235
236RfbPlayer::~RfbPlayer() {
237 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000238 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000239 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) {
254 switch (msg) {
255
256 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000257
258 case WM_CREATE:
259 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000260 // Create the frame window
261 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
262 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
263 hwnd, 0, frameClass.instance, this);
264
george82d070c692005-01-19 16:44:04 +0000265 createToolBar(hwnd);
266
george82006f2792005-02-05 07:40:47 +0000267 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000268
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000269 return 0;
270 }
271
george827214b822004-12-12 07:02:51 +0000272 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000273
274 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000275 switch (LOWORD(wParam)) {
276 case ID_PLAY:
277 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000278 break;
279 case ID_PAUSE:
280 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000281 break;
282 case ID_STOP:
283 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000284 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000285 }
george825c13c662005-01-27 14:48:23 +0000286 break;
287 case ID_PLAYPAUSE:
288 if (isPaused()) {
289 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000290 } else {
291 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000292 }
george825c13c662005-01-27 14:48:23 +0000293 break;
294 case ID_FULLSCREEN:
295 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
296 break;
george824ea27f62005-01-29 15:03:06 +0000297 case ID_RETURN:
298 // Update the speed if return pressed in speedEdit
299 if (speedEdit == GetFocus()) {
300 char speedStr[20], *stopStr;
301 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
302 double speed = strtod(speedStr, &stopStr);
303 if (speed > 0) {
304 speed = min(MAX_SPEED, speed);
305 // Update speedUpDown position
306 SendMessage(speedUpDown, UDM_SETPOS,
307 0, MAKELONG((short)(speed / 0.5), 0));
308 } else {
309 speed = getSpeed();
310 }
311 setSpeed(speed);
312 sprintf(speedStr, "%.2f", speed);
313 SetWindowText(speedEdit, speedStr);
314 }
315 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000316 }
317 break;
318
319 // Update frame's window size and add scrollbars if required
320
321 case WM_SIZE:
322 {
george82d070c692005-01-19 16:44:04 +0000323
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000324 Point old_offset = bufferToClient(Point(0, 0));
325
326 // Update the cached sizing information
327 RECT r;
328 GetClientRect(getMainHandle(), &r);
329 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
330 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
331
332 GetWindowRect(getFrameHandle(), &r);
333 window_size = Rect(r.left, r.top, r.right, r.bottom);
334 GetClientRect(getFrameHandle(), &r);
335 client_size = Rect(r.left, r.top, r.right, r.bottom);
336
337 // Determine whether scrollbars are required
338 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000339
340 // Resize the ToolBar
341 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000342
343 // Redraw if required
344 if (!old_offset.equals(bufferToClient(Point(0, 0))))
345 InvalidateRect(getFrameHandle(), 0, TRUE);
346 }
347 break;
george829e6e6cc2005-01-29 13:12:05 +0000348
349 case WM_NOTIFY:
350 switch (((NMHDR*)lParam)->code) {
351 case UDN_DELTAPOS:
352 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000353 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000354 char speedStr[20] = "\0";
355 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
356 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
357 double speed;
358
george824ea27f62005-01-29 15:03:06 +0000359 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000360 if (upDown->iDelta > 0) {
361 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
362 } else {
george824ea27f62005-01-29 15:03:06 +0000363 // It's need to round the UpDown position
364 if ((upDown->iPos * 0.5) != getSpeed()) {
365 upDown->iDelta = 0;
366 lResult = TRUE;
367 }
george829e6e6cc2005-01-29 13:12:05 +0000368 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
369 }
370 _gcvt(speed, 5, speedStr);
371 sprintf(speedStr, "%.2f", speed);
372 SetWindowText(speedEdit, speedStr);
373 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000374 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000375 }
george824ea27f62005-01-29 15:03:06 +0000376 }
george829e6e6cc2005-01-29 13:12:05 +0000377 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000378
379 case WM_CLOSE:
380 vlog.debug("WM_CLOSE %x", getMainHandle());
381 PostQuitMessage(0);
382 break;
383 }
384
385 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
386}
387
388LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
389 switch (msg) {
390
391 case WM_PAINT:
392 {
393 if (is->isSeeking()) {
394 seekMode = true;
395 return 0;
396 } else {
397 if (seekMode) {
398 seekMode = false;
399 InvalidateRect(getFrameHandle(), 0, true);
400 UpdateWindow(getFrameHandle());
401 return 0;
402 }
403 }
404
405 PAINTSTRUCT ps;
406 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
407 if (!paintDC)
408 throw SystemException("unable to BeginPaint", GetLastError());
409 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
410
411 if (!pr.is_empty()) {
412
413 if (buffer->bitmap) {
414
415 // Get device context
416 BitmapDC bitmapDC(paintDC, buffer->bitmap);
417
418 // Blit the border if required
419 Rect bufpos = bufferToClient(buffer->getRect());
420 if (!pr.enclosed_by(bufpos)) {
421 vlog.debug("draw border");
422 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
423 RECT r;
424 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
425 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
426 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
427 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
428 }
429
430 // Do the blit
431 Point buf_pos = clientToBuffer(pr.tl);
432 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
433 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
434 throw SystemException("unable to BitBlt to window", GetLastError());
435
436 } else {
437 // Blit a load of black
438 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
439 0, 0, 0, BLACKNESS))
440 throw SystemException("unable to BitBlt to blank window", GetLastError());
441 }
442 }
443 EndPaint(getFrameHandle(), &ps);
444 }
445 return 0;
446
447 case WM_VSCROLL:
448 case WM_HSCROLL:
449 {
450 Point delta;
451 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
452
453 switch (LOWORD(wParam)) {
454 case SB_PAGEUP: newpos -= 50; break;
455 case SB_PAGEDOWN: newpos += 50; break;
456 case SB_LINEUP: newpos -= 5; break;
457 case SB_LINEDOWN: newpos += 5; break;
458 case SB_THUMBTRACK:
459 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
460 default: vlog.info("received unknown scroll message");
461 };
462
463 if (msg == WM_HSCROLL)
464 setViewportOffset(Point(newpos, scrolloffset.y));
465 else
466 setViewportOffset(Point(scrolloffset.x, newpos));
467
468 SCROLLINFO si;
469 si.cbSize = sizeof(si);
470 si.fMask = SIF_POS;
471 si.nPos = newpos;
472 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
473 }
474 break;
475 }
476
477 return DefWindowProc(hwnd, msg, wParam, lParam);
478}
479
480void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
481 bool _autoplay = false, bool _showControls = true) {
482 showControls = _showControls;
483 autoplay = _autoplay;
484 playbackSpeed = _playbackSpeed;
485 initTime = _initTime;
486}
487
488void RfbPlayer::applyOptions() {
489 if (initTime >= 0)
490 setPos(initTime);
491 setSpeed(playbackSpeed);
492 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000493}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000494
george82d070c692005-01-19 16:44:04 +0000495void RfbPlayer::createToolBar(HWND parentHwnd) {
496 RECT tRect;
497 InitCommonControls();
498
499 tb.create(ID_TOOLBAR, parentHwnd);
500 tb.addBitmap(4, IDB_TOOLBAR);
501
502 // Create the control buttons
503 tb.addButton(0, ID_PLAY);
504 tb.addButton(1, ID_PAUSE);
505 tb.addButton(2, ID_STOP);
506 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
507 tb.addButton(3, ID_FULLSCREEN);
508 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
509
510 // Create the static control for the time output
511 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
512 tb.getButtonRect(6, &tRect);
513 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
514 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
515 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
516 GetModuleHandle(0), 0);
517 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
518
519 // Create the trackbar control for the time position
520 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
521 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000522 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000523 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
524 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
525 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
526 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000527 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000528 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
529
530 // Create the label with "Speed:" caption
531 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
532 tb.getButtonRect(10, &tRect);
533 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
534 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
535 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
536
537 // Create the edit control and the spin for the speed managing
538 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
539 tb.getButtonRect(11, &tRect);
540 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
541 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
542 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
543 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
544 // It's need to send notify messages to toolbar parent window
545 SetParent(speedEdit, tb.getHandle());
546
547 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
548 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000549 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000550}
551
552void RfbPlayer::setVisible(bool visible) {
553 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
554 if (visible) {
555 // When the window becomes visible, make it active
556 SetForegroundWindow(getMainHandle());
557 SetActiveWindow(getMainHandle());
558 }
559}
560
561void RfbPlayer::setTitle(const char *title) {
562 char _title[256];
563 strcpy(_title, AppName);
564 strcat(_title, " - ");
565 strcat(_title, title);
566 SetWindowText(getMainHandle(), _title);
567}
568
569void RfbPlayer::setFrameSize(int width, int height) {
570 // Calculate and set required size for main window
571 RECT r = {0, 0, width, height};
572 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
573 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
574 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
575 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
576 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
577 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
578
579 // Enable/disable scrollbars as appropriate
580 calculateScrollBars();
581}
582
583void RfbPlayer::calculateScrollBars() {
584 // Calculate the required size of window
585 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
586 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
587 DWORD old_style;
588 RECT r;
589 SetRect(&r, 0, 0, buffer->width(), buffer->height());
590 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
591 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
592
593 // Work out whether scroll bars are required
594 do {
595 old_style = style;
596
597 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
598 style |= WS_HSCROLL;
599 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
600 }
601 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
602 style |= WS_VSCROLL;
603 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
604 }
605 } while (style != old_style);
606
607 // Tell Windows to update the window style & cached settings
608 if (style != current_style) {
609 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
610 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
611 }
612
613 // Update the scroll settings
614 SCROLLINFO si;
615 if (style & WS_VSCROLL) {
616 si.cbSize = sizeof(si);
617 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
618 si.nMin = 0;
619 si.nMax = buffer->height();
620 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
621 maxscrolloffset.y = max(0, si.nMax-si.nPage);
622 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
623 si.nPos = scrolloffset.y;
624 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
625 }
626 if (style & WS_HSCROLL) {
627 si.cbSize = sizeof(si);
628 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
629 si.nMin = 0;
630 si.nMax = buffer->width();
631 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
632 maxscrolloffset.x = max(0, si.nMax-si.nPage);
633 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
634 si.nPos = scrolloffset.x;
635 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
636 }
637}
638
639bool RfbPlayer::setViewportOffset(const Point& tl) {
640/* ***
641 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
642 max(0, min(maxscrolloffset.y, tl.y)));
643 */
644 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
645 max(0, min(tl.y, buffer->height()-client_size.height())));
646 Point delta = np.translate(scrolloffset.negate());
647 if (!np.equals(scrolloffset)) {
648 scrolloffset = np;
649 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
650 UpdateWindow(getFrameHandle());
651 return true;
652 }
653 return false;
654}
655
656void RfbPlayer::close(const char* reason) {
657 setVisible(false);
658 if (reason) {
659 vlog.info("closing - %s", reason);
660 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
661 }
662 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
663}
664
665void RfbPlayer::blankBuffer() {
666 fillRect(buffer->getRect(), 0);
667}
668
669void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000670 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000671 blankBuffer();
672 newSession(fileName);
673 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000674 setSpeed(playbackSpeed);
george82006f2792005-02-05 07:40:47 +0000675 is->pausePlayback();
george8223e08562005-01-31 15:16:42 +0000676}
677
678void RfbPlayer::processMsg() {
679 static long update_time = GetTickCount();
680 try {
681 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)) {
682 // Update pos in the toolbar 4 times in 1 second
683 updatePos();
684 update_time = GetTickCount();
685 }
686 RfbProto::processMsg();
687 } catch (rdr::Exception e) {
688 if (strcmp(e.str(), "[End Of File]") == 0) {
689 rewind();
690 setPaused(true);
george8223e08562005-01-31 15:16:42 +0000691 return;
692 }
693 // It's a special exception to perform backward seeking.
694 // We only rewind the stream and seek the offset
695 if (strcmp(e.str(), "[REWIND]") == 0) {
696 long initTime = getSeekOffset();
697 rewind();
698 setPos(initTime);
699 } else {
700 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
701 return;
702 }
703 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000704}
705
706void RfbPlayer::serverInit() {
707 RfbProto::serverInit();
708
709 // Save the server init time for using in setPos()
710 serverInitTime = getTimeOffset() / getSpeed();
711
712 // Resize the backing buffer
713 buffer->setSize(cp.width, cp.height);
714
715 // Check on the true colour mode
716 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000717 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000718
719 // Set the session pixel format
720 buffer->setPF(cp.pf());
721
722 // If the window is not maximised then resize it
723 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
724 setFrameSize(cp.width, cp.height);
725
726 // Set the window title and show it
727 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000728
george82d4d69e62005-02-05 09:23:18 +0000729 // Calculate the full session time and update posTrackBar control
730 long sec = calculateSessionTime(fileName);
731 sprintf(fullSessionTime, "%.2um:%.2us", sec / 60, sec % 60);
732 updatePos();
733 SendMessage(posTrackBar, TBM_SETRANGE,
734 TRUE, MAKELONG(0, min(sec, MAX_POS_TRACKBAR_RANGE)));
735
george82006f2792005-02-05 07:40:47 +0000736 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000737}
738
739void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
740 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
741 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
742/* int i;
743 for (i=0;i<count;i++) {
744 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
745 }
746 // *** change to 0, 256?
747 refreshWindowPalette(first, count);
748 palette_changed = true;
749 InvalidateRect(getFrameHandle(), 0, FALSE);*/
750}
751
752void RfbPlayer::bell() {
753 if (acceptBell)
754 MessageBeep(-1);
755}
756
757void RfbPlayer::serverCutText(const char* str, int len) {
758 if (cutText != NULL)
759 delete [] cutText;
760 cutText = new char[len + 1];
761 memcpy(cutText, str, len);
762 cutText[len] = '\0';
763}
764
765void RfbPlayer::frameBufferUpdateEnd() {
766};
767
768void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
769}
770
771void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
772}
773
774
775void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
776 buffer->fillRect(r, pix);
777 invalidateBufferRect(r);
778}
779
780void RfbPlayer::imageRect(const Rect& r, void* pixels) {
781 buffer->imageRect(r, pixels);
782 invalidateBufferRect(r);
783}
784
785void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
786 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
787 invalidateBufferRect(r);
788}
789
790bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
791 Rect rect = bufferToClient(crect);
792 if (rect.intersect(client_size).is_empty()) return false;
793 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
794 InvalidateRect(getFrameHandle(), &invalid, FALSE);
795 return true;
796}
797
george8257f13522005-02-05 08:48:22 +0000798long RfbPlayer::calculateSessionTime(char *filename) {
799 FbsInputStream sessionFile(filename);
800 sessionFile.setSpeed(1000);
801 try {
802 while (TRUE) {
803 sessionFile.skip(1024);
804 }
805 } catch (rdr::Exception e) {
806 if (strcmp(e.str(), "[End Of File]") == 0) {
807 return sessionFile.getTimeOffset() / 1000;
808 } else {
809 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
810 return 0;
811 }
812 }
813 return 0;
814}
815
george8217e92cb2005-01-31 16:01:02 +0000816void RfbPlayer::openSessionFile(char *_fileName) {
817 fileName = strDup(_fileName);
818
819 // Close the previous reading thread
820 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000821 delete rfbReader->join();
822 }
823 blankBuffer();
824 newSession(fileName);
825 setSpeed(playbackSpeed);
826 rfbReader = new rfbSessionReader(this);
827 rfbReader->start();
828}
829
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000830void RfbPlayer::setPaused(bool paused) {
831 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000832 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000833 tb.checkButton(ID_PAUSE, true);
834 tb.checkButton(ID_PLAY, false);
835 tb.checkButton(ID_STOP, false);
836 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
837 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000838 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000839 is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000840 tb.checkButton(ID_PLAY, true);
841 tb.checkButton(ID_STOP, false);
842 tb.checkButton(ID_PAUSE, false);
843 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
844 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000845 }
846}
847
george82006f2792005-02-05 07:40:47 +0000848void RfbPlayer::stopPlayback() {
849 setPos(0);
850 is->pausePlayback();
851 tb.checkButton(ID_STOP, true);
852 tb.checkButton(ID_PLAY, false);
853 tb.checkButton(ID_PAUSE, false);
854 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
855 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
856}
857
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000858void RfbPlayer::setSpeed(double speed) {
859 serverInitTime = serverInitTime * getSpeed() / speed;
860 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000861 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000862}
863
864double RfbPlayer::getSpeed() {
865 return is->getSpeed();
866}
867
868void RfbPlayer::setPos(long pos) {
869 is->setTimeOffset(max(pos, serverInitTime));
870}
871
872long RfbPlayer::getSeekOffset() {
873 return is->getSeekOffset();
874}
875
876bool RfbPlayer::isSeeking() {
877 return is->isSeeking();
878}
879
880bool RfbPlayer::isSeekMode() {
881 return seekMode;
882}
883
884bool RfbPlayer::isPaused() {
885 return is->isPaused();
886}
887
888long RfbPlayer::getTimeOffset() {
889 return is->getTimeOffset();
890}
891
892void RfbPlayer::updatePos() {
george823c8fbbf2005-01-24 11:09:08 +0000893 char timePos[30] = "\0";
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000894 long newPos = is->getTimeOffset() / 1000;
george823c8fbbf2005-01-24 11:09:08 +0000895 time_pos_m = newPos / 60;
896 time_pos_s = newPos % 60;
897 if (time_pos_m < 10) {
898 strcat(timePos, "0");
899 _itoa(time_pos_m, timePos+1, 10);
900 } else {
901 _itoa(time_pos_m, timePos, 10);
902 }
903 strcat(timePos, "m:");
904 if (time_pos_s < 10) {
905 strcat(timePos, "0");
906 _itoa(time_pos_s, timePos+strlen(timePos), 10);
907 } else {
908 _itoa(time_pos_s, timePos+strlen(timePos), 10);
909 }
910 strcat(timePos, "s ");
911 strcat(timePos, "(");
912 strcat(timePos, fullSessionTime);
913 strcat(timePos, ")");
914 SetWindowText(timeStatic, timePos);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000915}
916
917void RfbPlayer::skipHandshaking() {
918 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
919 is->skip(skipBytes);
920 state_ = RFBSTATE_NORMAL;
921}
922
923void programInfo() {
924 win32::FileVersionInfo inf;
925 _tprintf(_T("%s - %s, Version %s\n"),
926 inf.getVerString(_T("ProductName")),
927 inf.getVerString(_T("FileDescription")),
928 inf.getVerString(_T("FileVersion")));
929 printf("%s\n", buildTime);
930 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
931}
932
933void programUsage() {
934 printf("usage: rfbplayer <options> <filename>\n");
935 printf("Command-line options:\n");
936 printf(" -help - Provide usage information.\n");
937 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
938 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
939 printf(" -pos <ms> - Sets initial time position in the session file,\n");
940 printf(" in milliseconds. Default: 0.\n");
941 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
942 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
943 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
944}
945
946double playbackSpeed = 1.0;
947long initTime = -1;
948bool autoplay = false;
949bool showControls = true;
950char *fileName;
951bool console = false;
952bool wrong_param = false;
953bool print_usage = false;
954bool acceptBell = false;
955
956bool processParams(int argc, char* argv[]) {
957 for (int i = 1; i < argc; i++) {
958 if ((strcasecmp(argv[i], "-help") == 0) ||
959 (strcasecmp(argv[i], "--help") == 0) ||
960 (strcasecmp(argv[i], "/help") == 0) ||
961 (strcasecmp(argv[i], "-h") == 0) ||
962 (strcasecmp(argv[i], "/h") == 0) ||
963 (strcasecmp(argv[i], "/?") == 0)) {
964 print_usage = true;
965 return true;
966 }
967
968 if ((strcasecmp(argv[i], "-speed") == 0) ||
969 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
970 playbackSpeed = atof(argv[++i]);
971 if (playbackSpeed <= 0) {
972 return false;
973 }
974 continue;
975 }
976
977 if ((strcasecmp(argv[i], "-pos") == 0) ||
978 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
979 initTime = atol(argv[++i]);
980 if (initTime <= 0)
981 return false;
982 continue;
983 }
984
985 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
986 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
987 i++;
988 if (strcasecmp(argv[i], "yes") == 0) {
989 autoplay = true;
990 continue;
991 }
992 if (strcasecmp(argv[i], "no") == 0) {
993 autoplay = false;
994 continue;
995 }
996 return false;
997 }
998
999 if ((strcasecmp(argv[i], "-controls") == 0) ||
1000 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
1001 i++;
1002 if (strcasecmp(argv[i], "yes") == 0) {
1003 showControls = true;
1004 continue;
1005 }
1006 if (strcasecmp(argv[i], "no") == 0) {
1007 showControls = false;
1008 continue;
1009 }
1010 return false;
1011 }
1012
1013 if ((strcasecmp(argv[i], "-bell") == 0) ||
1014 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
1015 i++;
1016 if (strcasecmp(argv[i], "yes") == 0) {
1017 acceptBell = true;
1018 continue;
1019 }
1020 if (strcasecmp(argv[i], "no") == 0) {
1021 acceptBell = false;
1022 continue;
1023 }
1024 return false;
1025 }
1026
1027 if (i != argc - 1)
1028 return false;
1029 }
1030
1031 fileName = strDup(argv[argc-1]);
1032 return true;
1033}
1034
1035//
1036// -=- WinMain
1037//
1038
1039int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1040
1041 // - Process the command-line
1042
1043 int argc = __argc;
1044 char** argv = __argv;
1045 if (argc > 1) {
1046 wrong_param = !processParams(argc, argv);
1047 console = print_usage | wrong_param;
1048 } else {
1049 console = true;
1050 }
1051
1052 if (console) {
1053 AllocConsole();
1054 freopen("CONOUT$","wb",stdout);
1055
1056 programInfo();
1057 if (wrong_param)
1058 printf("Wrong a command line.\n");
1059 else
1060 programUsage();
1061
1062 printf("\nPress Enter/Return key to continue\n");
1063 char c = getch();
1064 FreeConsole();
1065
1066 return 0;
george8267cbcd02005-01-16 15:39:56 +00001067 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001068
1069 // Create the player and the thread which reading the rfb data
1070 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001071 try {
1072 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
1073 showControls, acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001074 } catch (rdr::Exception e) {
1075 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1076 delete player;
1077 return 0;
1078 }
1079
1080 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001081 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001082 MSG msg;
1083 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001084 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1085 TranslateMessage(&msg);
1086 DispatchMessage(&msg);
1087 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001088 }
1089
1090 // Wait while the thread destroying and then destroy the player
1091 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001092 if (player) delete player;
1093 } catch (rdr::Exception e) {
1094 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1095 }
1096
1097 return 0;
1098};