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