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