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