blob: 53c78eea80f5d98d163dbbfe74b39fc029d38b23 [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
47
george82d070c692005-01-19 16:44:04 +000048#define ID_TOOLBAR 500
49#define ID_PLAY 510
50#define ID_PAUSE 520
51#define ID_TIME_STATIC 530
52#define ID_SPEED_STATIC 540
53#define ID_SPEED_EDIT 550
54#define ID_POS_TRACKBAR 560
55#define ID_SPEED_UPDOWN 570
56
57
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000058//
59// -=- RfbPlayerClass
60
61//
62// Window class used as the basis for RfbPlayer instance
63//
64
65class RfbPlayerClass {
66public:
67 RfbPlayerClass();
68 ~RfbPlayerClass();
69 ATOM classAtom;
70 HINSTANCE instance;
71};
72
73LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
74 LRESULT result;
75
76 if (msg == WM_CREATE)
77 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
78 else if (msg == WM_DESTROY) {
79 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
george8267cbcd02005-01-16 15:39:56 +000080 _this->fRun = false;
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),
george8267cbcd02005-01-16 15:39:56 +0000204 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName), fRun(true),
george82d070c692005-01-19 16:44:04 +0000205 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), speedTrackBar(0),
206 speedUpDown(0), acceptBell(_acceptBell) {
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
213 // Create the main window
214 const TCHAR* name = _T("RfbPlayer");
215 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000216 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000217 if (!mainHwnd) {
218 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
219 }
220 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
221
222 // Create the backing buffer
223 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000224 setVisible(true);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000225}
226
227RfbPlayer::~RfbPlayer() {
228 vlog.debug("~RfbPlayer");
229 if (mainHwnd) {
230 setVisible(false);
231 DestroyWindow(mainHwnd);
232 mainHwnd = 0;
233 }
234 delete buffer;
235 delete cutText;
236 vlog.debug("~RfbPlayer done");
237}
238
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000239LRESULT
240RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
241 switch (msg) {
242
243 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000244
245 case WM_CREATE:
246 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000247 // Create the frame window
248 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
249 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
250 hwnd, 0, frameClass.instance, this);
251
george82d070c692005-01-19 16:44:04 +0000252 createToolBar(hwnd);
253
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000254 return 0;
255 }
256
george827214b822004-12-12 07:02:51 +0000257 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000258
259 case WM_COMMAND:
260 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000261
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000262 }
263 break;
264
265 // Update frame's window size and add scrollbars if required
266
267 case WM_SIZE:
268 {
george82d070c692005-01-19 16:44:04 +0000269
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000270 Point old_offset = bufferToClient(Point(0, 0));
271
272 // Update the cached sizing information
273 RECT r;
274 GetClientRect(getMainHandle(), &r);
275 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
276 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
277
278 GetWindowRect(getFrameHandle(), &r);
279 window_size = Rect(r.left, r.top, r.right, r.bottom);
280 GetClientRect(getFrameHandle(), &r);
281 client_size = Rect(r.left, r.top, r.right, r.bottom);
282
283 // Determine whether scrollbars are required
284 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000285
286 // Resize the ToolBar
287 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000288
289 // Redraw if required
290 if (!old_offset.equals(bufferToClient(Point(0, 0))))
291 InvalidateRect(getFrameHandle(), 0, TRUE);
292 }
293 break;
294
295 case WM_CLOSE:
296 vlog.debug("WM_CLOSE %x", getMainHandle());
297 PostQuitMessage(0);
298 break;
299 }
300
301 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
302}
303
304LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
305 switch (msg) {
306
307 case WM_PAINT:
308 {
309 if (is->isSeeking()) {
310 seekMode = true;
311 return 0;
312 } else {
313 if (seekMode) {
314 seekMode = false;
315 InvalidateRect(getFrameHandle(), 0, true);
316 UpdateWindow(getFrameHandle());
317 return 0;
318 }
319 }
320
321 PAINTSTRUCT ps;
322 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
323 if (!paintDC)
324 throw SystemException("unable to BeginPaint", GetLastError());
325 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
326
327 if (!pr.is_empty()) {
328
329 if (buffer->bitmap) {
330
331 // Get device context
332 BitmapDC bitmapDC(paintDC, buffer->bitmap);
333
334 // Blit the border if required
335 Rect bufpos = bufferToClient(buffer->getRect());
336 if (!pr.enclosed_by(bufpos)) {
337 vlog.debug("draw border");
338 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
339 RECT r;
340 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
341 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
342 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
343 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
344 }
345
346 // Do the blit
347 Point buf_pos = clientToBuffer(pr.tl);
348 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
349 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
350 throw SystemException("unable to BitBlt to window", GetLastError());
351
352 } else {
353 // Blit a load of black
354 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
355 0, 0, 0, BLACKNESS))
356 throw SystemException("unable to BitBlt to blank window", GetLastError());
357 }
358 }
359 EndPaint(getFrameHandle(), &ps);
360 }
361 return 0;
362
363 case WM_VSCROLL:
364 case WM_HSCROLL:
365 {
366 Point delta;
367 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
368
369 switch (LOWORD(wParam)) {
370 case SB_PAGEUP: newpos -= 50; break;
371 case SB_PAGEDOWN: newpos += 50; break;
372 case SB_LINEUP: newpos -= 5; break;
373 case SB_LINEDOWN: newpos += 5; break;
374 case SB_THUMBTRACK:
375 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
376 default: vlog.info("received unknown scroll message");
377 };
378
379 if (msg == WM_HSCROLL)
380 setViewportOffset(Point(newpos, scrolloffset.y));
381 else
382 setViewportOffset(Point(scrolloffset.x, newpos));
383
384 SCROLLINFO si;
385 si.cbSize = sizeof(si);
386 si.fMask = SIF_POS;
387 si.nPos = newpos;
388 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
389 }
390 break;
391 }
392
393 return DefWindowProc(hwnd, msg, wParam, lParam);
394}
395
george8267cbcd02005-01-16 15:39:56 +0000396void RfbPlayer::run() {
397 long initTime = -1;
398
399 // Process the rfb messages
400 while (fRun) {
401 try {
402 if (initTime >= 0) {
403 setPos(initTime);
404 initTime = -1;
405 }
406 if (!isSeeking())
407 updatePos();
408 processMsg();
409 } catch (rdr::Exception e) {
410 if (strcmp(e.str(), "[End Of File]") == 0) {
411 rewind();
412 setPaused(true);
413 continue;
414 }
415 // It's a special exception to perform backward seeking.
416 // We only rewind the stream and seek the offset
417 if (strcmp(e.str(), "[REWIND]") == 0) {
418 initTime = getSeekOffset();
419 double speed = getSpeed();
420 bool play = !isPaused();
421 rewind();
422 setSpeed(speed);
423 setPaused(!play);
424 } else {
425 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
426 return;
427 }
428 }
429 }
430}
431
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000432void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
433 bool _autoplay = false, bool _showControls = true) {
434 showControls = _showControls;
435 autoplay = _autoplay;
436 playbackSpeed = _playbackSpeed;
437 initTime = _initTime;
438}
439
440void RfbPlayer::applyOptions() {
441 if (initTime >= 0)
442 setPos(initTime);
443 setSpeed(playbackSpeed);
444 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000445}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000446
george82d070c692005-01-19 16:44:04 +0000447void RfbPlayer::createToolBar(HWND parentHwnd) {
448 RECT tRect;
449 InitCommonControls();
450
451 tb.create(ID_TOOLBAR, parentHwnd);
452 tb.addBitmap(4, IDB_TOOLBAR);
453
454 // Create the control buttons
455 tb.addButton(0, ID_PLAY);
456 tb.addButton(1, ID_PAUSE);
457 tb.addButton(2, ID_STOP);
458 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
459 tb.addButton(3, ID_FULLSCREEN);
460 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
461
462 // Create the static control for the time output
463 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
464 tb.getButtonRect(6, &tRect);
465 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
466 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
467 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
468 GetModuleHandle(0), 0);
469 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
470
471 // Create the trackbar control for the time position
472 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
473 tb.getButtonRect(8, &tRect);
474 speedTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
475 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
476 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
477 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
478 // It's need to send notify messages to toolbar parent window
479 SetParent(speedTrackBar, tb.getHandle());
480 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
481
482 // Create the label with "Speed:" caption
483 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
484 tb.getButtonRect(10, &tRect);
485 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
486 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
487 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
488
489 // Create the edit control and the spin for the speed managing
490 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
491 tb.getButtonRect(11, &tRect);
492 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
493 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
494 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
495 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
496 // It's need to send notify messages to toolbar parent window
497 SetParent(speedEdit, tb.getHandle());
498
499 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
500 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
501 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 100, 1, 10);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000502}
503
504void RfbPlayer::setVisible(bool visible) {
505 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
506 if (visible) {
507 // When the window becomes visible, make it active
508 SetForegroundWindow(getMainHandle());
509 SetActiveWindow(getMainHandle());
510 }
511}
512
513void RfbPlayer::setTitle(const char *title) {
514 char _title[256];
515 strcpy(_title, AppName);
516 strcat(_title, " - ");
517 strcat(_title, title);
518 SetWindowText(getMainHandle(), _title);
519}
520
521void RfbPlayer::setFrameSize(int width, int height) {
522 // Calculate and set required size for main window
523 RECT r = {0, 0, width, height};
524 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
525 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
526 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
527 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
528 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
529 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
530
531 // Enable/disable scrollbars as appropriate
532 calculateScrollBars();
533}
534
535void RfbPlayer::calculateScrollBars() {
536 // Calculate the required size of window
537 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
538 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
539 DWORD old_style;
540 RECT r;
541 SetRect(&r, 0, 0, buffer->width(), buffer->height());
542 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
543 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
544
545 // Work out whether scroll bars are required
546 do {
547 old_style = style;
548
549 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
550 style |= WS_HSCROLL;
551 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
552 }
553 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
554 style |= WS_VSCROLL;
555 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
556 }
557 } while (style != old_style);
558
559 // Tell Windows to update the window style & cached settings
560 if (style != current_style) {
561 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
562 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
563 }
564
565 // Update the scroll settings
566 SCROLLINFO si;
567 if (style & WS_VSCROLL) {
568 si.cbSize = sizeof(si);
569 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
570 si.nMin = 0;
571 si.nMax = buffer->height();
572 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
573 maxscrolloffset.y = max(0, si.nMax-si.nPage);
574 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
575 si.nPos = scrolloffset.y;
576 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
577 }
578 if (style & WS_HSCROLL) {
579 si.cbSize = sizeof(si);
580 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
581 si.nMin = 0;
582 si.nMax = buffer->width();
583 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
584 maxscrolloffset.x = max(0, si.nMax-si.nPage);
585 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
586 si.nPos = scrolloffset.x;
587 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
588 }
589}
590
591bool RfbPlayer::setViewportOffset(const Point& tl) {
592/* ***
593 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
594 max(0, min(maxscrolloffset.y, tl.y)));
595 */
596 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
597 max(0, min(tl.y, buffer->height()-client_size.height())));
598 Point delta = np.translate(scrolloffset.negate());
599 if (!np.equals(scrolloffset)) {
600 scrolloffset = np;
601 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
602 UpdateWindow(getFrameHandle());
603 return true;
604 }
605 return false;
606}
607
608void RfbPlayer::close(const char* reason) {
609 setVisible(false);
610 if (reason) {
611 vlog.info("closing - %s", reason);
612 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
613 }
614 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
615}
616
617void RfbPlayer::blankBuffer() {
618 fillRect(buffer->getRect(), 0);
619}
620
621void RfbPlayer::rewind() {
622 blankBuffer();
623 newSession(fileName);
624 skipHandshaking();
625}
626
627void RfbPlayer::serverInit() {
628 RfbProto::serverInit();
629
630 // Save the server init time for using in setPos()
631 serverInitTime = getTimeOffset() / getSpeed();
632
633 // Resize the backing buffer
634 buffer->setSize(cp.width, cp.height);
635
636 // Check on the true colour mode
637 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000638 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000639
640 // Set the session pixel format
641 buffer->setPF(cp.pf());
642
643 // If the window is not maximised then resize it
644 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
645 setFrameSize(cp.width, cp.height);
646
647 // Set the window title and show it
648 setTitle(cp.name());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000649
650 // Set the player's param
651 applyOptions();
652}
653
654void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
655 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
656 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
657/* int i;
658 for (i=0;i<count;i++) {
659 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
660 }
661 // *** change to 0, 256?
662 refreshWindowPalette(first, count);
663 palette_changed = true;
664 InvalidateRect(getFrameHandle(), 0, FALSE);*/
665}
666
667void RfbPlayer::bell() {
668 if (acceptBell)
669 MessageBeep(-1);
670}
671
672void RfbPlayer::serverCutText(const char* str, int len) {
673 if (cutText != NULL)
674 delete [] cutText;
675 cutText = new char[len + 1];
676 memcpy(cutText, str, len);
677 cutText[len] = '\0';
678}
679
680void RfbPlayer::frameBufferUpdateEnd() {
681};
682
683void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
684}
685
686void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
687}
688
689
690void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
691 buffer->fillRect(r, pix);
692 invalidateBufferRect(r);
693}
694
695void RfbPlayer::imageRect(const Rect& r, void* pixels) {
696 buffer->imageRect(r, pixels);
697 invalidateBufferRect(r);
698}
699
700void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
701 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
702 invalidateBufferRect(r);
703}
704
705bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
706 Rect rect = bufferToClient(crect);
707 if (rect.intersect(client_size).is_empty()) return false;
708 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
709 InvalidateRect(getFrameHandle(), &invalid, FALSE);
710 return true;
711}
712
713void RfbPlayer::setPaused(bool paused) {
714 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000715 is->pausePlayback();
716 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000717 is->resumePlayback();
718 }
719}
720
721void RfbPlayer::setSpeed(double speed) {
722 serverInitTime = serverInitTime * getSpeed() / speed;
723 is->setSpeed(speed);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000724}
725
726double RfbPlayer::getSpeed() {
727 return is->getSpeed();
728}
729
730void RfbPlayer::setPos(long pos) {
731 is->setTimeOffset(max(pos, serverInitTime));
732}
733
734long RfbPlayer::getSeekOffset() {
735 return is->getSeekOffset();
736}
737
738bool RfbPlayer::isSeeking() {
739 return is->isSeeking();
740}
741
742bool RfbPlayer::isSeekMode() {
743 return seekMode;
744}
745
746bool RfbPlayer::isPaused() {
747 return is->isPaused();
748}
749
750long RfbPlayer::getTimeOffset() {
751 return is->getTimeOffset();
752}
753
754void RfbPlayer::updatePos() {
755 long newPos = is->getTimeOffset() / 1000;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000756}
757
758void RfbPlayer::skipHandshaking() {
759 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
760 is->skip(skipBytes);
761 state_ = RFBSTATE_NORMAL;
762}
763
764void programInfo() {
765 win32::FileVersionInfo inf;
766 _tprintf(_T("%s - %s, Version %s\n"),
767 inf.getVerString(_T("ProductName")),
768 inf.getVerString(_T("FileDescription")),
769 inf.getVerString(_T("FileVersion")));
770 printf("%s\n", buildTime);
771 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
772}
773
774void programUsage() {
775 printf("usage: rfbplayer <options> <filename>\n");
776 printf("Command-line options:\n");
777 printf(" -help - Provide usage information.\n");
778 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
779 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
780 printf(" -pos <ms> - Sets initial time position in the session file,\n");
781 printf(" in milliseconds. Default: 0.\n");
782 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
783 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
784 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
785}
786
787double playbackSpeed = 1.0;
788long initTime = -1;
789bool autoplay = false;
790bool showControls = true;
791char *fileName;
792bool console = false;
793bool wrong_param = false;
794bool print_usage = false;
795bool acceptBell = false;
796
797bool processParams(int argc, char* argv[]) {
798 for (int i = 1; i < argc; i++) {
799 if ((strcasecmp(argv[i], "-help") == 0) ||
800 (strcasecmp(argv[i], "--help") == 0) ||
801 (strcasecmp(argv[i], "/help") == 0) ||
802 (strcasecmp(argv[i], "-h") == 0) ||
803 (strcasecmp(argv[i], "/h") == 0) ||
804 (strcasecmp(argv[i], "/?") == 0)) {
805 print_usage = true;
806 return true;
807 }
808
809 if ((strcasecmp(argv[i], "-speed") == 0) ||
810 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
811 playbackSpeed = atof(argv[++i]);
812 if (playbackSpeed <= 0) {
813 return false;
814 }
815 continue;
816 }
817
818 if ((strcasecmp(argv[i], "-pos") == 0) ||
819 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
820 initTime = atol(argv[++i]);
821 if (initTime <= 0)
822 return false;
823 continue;
824 }
825
826 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
827 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
828 i++;
829 if (strcasecmp(argv[i], "yes") == 0) {
830 autoplay = true;
831 continue;
832 }
833 if (strcasecmp(argv[i], "no") == 0) {
834 autoplay = false;
835 continue;
836 }
837 return false;
838 }
839
840 if ((strcasecmp(argv[i], "-controls") == 0) ||
841 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
842 i++;
843 if (strcasecmp(argv[i], "yes") == 0) {
844 showControls = true;
845 continue;
846 }
847 if (strcasecmp(argv[i], "no") == 0) {
848 showControls = false;
849 continue;
850 }
851 return false;
852 }
853
854 if ((strcasecmp(argv[i], "-bell") == 0) ||
855 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
856 i++;
857 if (strcasecmp(argv[i], "yes") == 0) {
858 acceptBell = true;
859 continue;
860 }
861 if (strcasecmp(argv[i], "no") == 0) {
862 acceptBell = false;
863 continue;
864 }
865 return false;
866 }
867
868 if (i != argc - 1)
869 return false;
870 }
871
872 fileName = strDup(argv[argc-1]);
873 return true;
874}
875
876//
877// -=- WinMain
878//
879
880int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
881
882 // - Process the command-line
883
884 int argc = __argc;
885 char** argv = __argv;
886 if (argc > 1) {
887 wrong_param = !processParams(argc, argv);
888 console = print_usage | wrong_param;
889 } else {
890 console = true;
891 }
892
893 if (console) {
894 AllocConsole();
895 freopen("CONOUT$","wb",stdout);
896
897 programInfo();
898 if (wrong_param)
899 printf("Wrong a command line.\n");
900 else
901 programUsage();
902
903 printf("\nPress Enter/Return key to continue\n");
904 char c = getch();
905 FreeConsole();
906
907 return 0;
george8267cbcd02005-01-16 15:39:56 +0000908 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000909
910 // Create the player and the thread which reading the rfb data
911 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000912 try {
913 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
914 showControls, acceptBell);
george8210313102005-01-17 13:11:40 +0000915 if (autoplay) player->start();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000916 } catch (rdr::Exception e) {
917 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
918 delete player;
919 return 0;
920 }
921
922 // Run the player
george825bbd61b2004-12-09 17:47:37 +0000923 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000924 MSG msg;
925 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +0000926 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
927 TranslateMessage(&msg);
928 DispatchMessage(&msg);
929 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000930 }
931
932 // Wait while the thread destroying and then destroy the player
933 try{
george8267cbcd02005-01-16 15:39:56 +0000934 while (player->getState() == ThreadStarted) {}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000935 if (player) delete player;
936 } catch (rdr::Exception e) {
937 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
938 }
939
940 return 0;
941};