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