blob: 94513d15b46a41a8fd1b6fea4834a47e115413fa [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;
401
402 // Process the rfb messages
403 while (fRun) {
404 try {
405 if (initTime >= 0) {
406 setPos(initTime);
407 initTime = -1;
408 }
409 if (!isSeeking())
410 updatePos();
411 processMsg();
412 } catch (rdr::Exception e) {
413 if (strcmp(e.str(), "[End Of File]") == 0) {
414 rewind();
415 setPaused(true);
416 continue;
417 }
418 // It's a special exception to perform backward seeking.
419 // We only rewind the stream and seek the offset
420 if (strcmp(e.str(), "[REWIND]") == 0) {
421 initTime = getSeekOffset();
422 double speed = getSpeed();
423 bool play = !isPaused();
424 rewind();
425 setSpeed(speed);
426 setPaused(!play);
427 } else {
428 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
429 return;
430 }
431 }
432 }
433}
434
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000435void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
436 bool _autoplay = false, bool _showControls = true) {
437 showControls = _showControls;
438 autoplay = _autoplay;
439 playbackSpeed = _playbackSpeed;
440 initTime = _initTime;
441}
442
443void RfbPlayer::applyOptions() {
444 if (initTime >= 0)
445 setPos(initTime);
446 setSpeed(playbackSpeed);
447 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000448}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000449
george82d070c692005-01-19 16:44:04 +0000450void RfbPlayer::createToolBar(HWND parentHwnd) {
451 RECT tRect;
452 InitCommonControls();
453
454 tb.create(ID_TOOLBAR, parentHwnd);
455 tb.addBitmap(4, IDB_TOOLBAR);
456
457 // Create the control buttons
458 tb.addButton(0, ID_PLAY);
459 tb.addButton(1, ID_PAUSE);
460 tb.addButton(2, ID_STOP);
461 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
462 tb.addButton(3, ID_FULLSCREEN);
463 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
464
465 // Create the static control for the time output
466 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
467 tb.getButtonRect(6, &tRect);
468 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
469 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
470 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
471 GetModuleHandle(0), 0);
472 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
473
474 // Create the trackbar control for the time position
475 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
476 tb.getButtonRect(8, &tRect);
477 speedTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
478 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
479 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
480 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
481 // It's need to send notify messages to toolbar parent window
482 SetParent(speedTrackBar, tb.getHandle());
483 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
484
485 // Create the label with "Speed:" caption
486 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
487 tb.getButtonRect(10, &tRect);
488 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
489 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
490 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
491
492 // Create the edit control and the spin for the speed managing
493 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
494 tb.getButtonRect(11, &tRect);
495 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
496 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
497 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
498 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
499 // It's need to send notify messages to toolbar parent window
500 SetParent(speedEdit, tb.getHandle());
501
502 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
503 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
504 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 100, 1, 10);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000505}
506
507void RfbPlayer::setVisible(bool visible) {
508 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
509 if (visible) {
510 // When the window becomes visible, make it active
511 SetForegroundWindow(getMainHandle());
512 SetActiveWindow(getMainHandle());
513 }
514}
515
516void RfbPlayer::setTitle(const char *title) {
517 char _title[256];
518 strcpy(_title, AppName);
519 strcat(_title, " - ");
520 strcat(_title, title);
521 SetWindowText(getMainHandle(), _title);
522}
523
524void RfbPlayer::setFrameSize(int width, int height) {
525 // Calculate and set required size for main window
526 RECT r = {0, 0, width, height};
527 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
528 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
529 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
530 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
531 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
532 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
533
534 // Enable/disable scrollbars as appropriate
535 calculateScrollBars();
536}
537
538void RfbPlayer::calculateScrollBars() {
539 // Calculate the required size of window
540 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
541 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
542 DWORD old_style;
543 RECT r;
544 SetRect(&r, 0, 0, buffer->width(), buffer->height());
545 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
546 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
547
548 // Work out whether scroll bars are required
549 do {
550 old_style = style;
551
552 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
553 style |= WS_HSCROLL;
554 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
555 }
556 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
557 style |= WS_VSCROLL;
558 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
559 }
560 } while (style != old_style);
561
562 // Tell Windows to update the window style & cached settings
563 if (style != current_style) {
564 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
565 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
566 }
567
568 // Update the scroll settings
569 SCROLLINFO si;
570 if (style & WS_VSCROLL) {
571 si.cbSize = sizeof(si);
572 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
573 si.nMin = 0;
574 si.nMax = buffer->height();
575 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
576 maxscrolloffset.y = max(0, si.nMax-si.nPage);
577 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
578 si.nPos = scrolloffset.y;
579 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
580 }
581 if (style & WS_HSCROLL) {
582 si.cbSize = sizeof(si);
583 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
584 si.nMin = 0;
585 si.nMax = buffer->width();
586 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
587 maxscrolloffset.x = max(0, si.nMax-si.nPage);
588 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
589 si.nPos = scrolloffset.x;
590 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
591 }
592}
593
594bool RfbPlayer::setViewportOffset(const Point& tl) {
595/* ***
596 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
597 max(0, min(maxscrolloffset.y, tl.y)));
598 */
599 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
600 max(0, min(tl.y, buffer->height()-client_size.height())));
601 Point delta = np.translate(scrolloffset.negate());
602 if (!np.equals(scrolloffset)) {
603 scrolloffset = np;
604 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
605 UpdateWindow(getFrameHandle());
606 return true;
607 }
608 return false;
609}
610
611void RfbPlayer::close(const char* reason) {
612 setVisible(false);
613 if (reason) {
614 vlog.info("closing - %s", reason);
615 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
616 }
617 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
618}
619
620void RfbPlayer::blankBuffer() {
621 fillRect(buffer->getRect(), 0);
622}
623
624void RfbPlayer::rewind() {
625 blankBuffer();
626 newSession(fileName);
627 skipHandshaking();
628}
629
630void RfbPlayer::serverInit() {
631 RfbProto::serverInit();
632
633 // Save the server init time for using in setPos()
634 serverInitTime = getTimeOffset() / getSpeed();
635
636 // Resize the backing buffer
637 buffer->setSize(cp.width, cp.height);
638
639 // Check on the true colour mode
640 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000641 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000642
643 // Set the session pixel format
644 buffer->setPF(cp.pf());
645
646 // If the window is not maximised then resize it
647 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
648 setFrameSize(cp.width, cp.height);
649
650 // Set the window title and show it
651 setTitle(cp.name());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000652
653 // Set the player's param
654 applyOptions();
655}
656
657void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
658 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
659 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
660/* int i;
661 for (i=0;i<count;i++) {
662 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
663 }
664 // *** change to 0, 256?
665 refreshWindowPalette(first, count);
666 palette_changed = true;
667 InvalidateRect(getFrameHandle(), 0, FALSE);*/
668}
669
670void RfbPlayer::bell() {
671 if (acceptBell)
672 MessageBeep(-1);
673}
674
675void RfbPlayer::serverCutText(const char* str, int len) {
676 if (cutText != NULL)
677 delete [] cutText;
678 cutText = new char[len + 1];
679 memcpy(cutText, str, len);
680 cutText[len] = '\0';
681}
682
683void RfbPlayer::frameBufferUpdateEnd() {
684};
685
686void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
687}
688
689void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
690}
691
692
693void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
694 buffer->fillRect(r, pix);
695 invalidateBufferRect(r);
696}
697
698void RfbPlayer::imageRect(const Rect& r, void* pixels) {
699 buffer->imageRect(r, pixels);
700 invalidateBufferRect(r);
701}
702
703void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
704 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
705 invalidateBufferRect(r);
706}
707
708bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
709 Rect rect = bufferToClient(crect);
710 if (rect.intersect(client_size).is_empty()) return false;
711 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
712 InvalidateRect(getFrameHandle(), &invalid, FALSE);
713 return true;
714}
715
716void RfbPlayer::setPaused(bool paused) {
717 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000718 is->pausePlayback();
719 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000720 is->resumePlayback();
721 }
722}
723
724void RfbPlayer::setSpeed(double speed) {
725 serverInitTime = serverInitTime * getSpeed() / speed;
726 is->setSpeed(speed);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000727}
728
729double RfbPlayer::getSpeed() {
730 return is->getSpeed();
731}
732
733void RfbPlayer::setPos(long pos) {
734 is->setTimeOffset(max(pos, serverInitTime));
735}
736
737long RfbPlayer::getSeekOffset() {
738 return is->getSeekOffset();
739}
740
741bool RfbPlayer::isSeeking() {
742 return is->isSeeking();
743}
744
745bool RfbPlayer::isSeekMode() {
746 return seekMode;
747}
748
749bool RfbPlayer::isPaused() {
750 return is->isPaused();
751}
752
753long RfbPlayer::getTimeOffset() {
754 return is->getTimeOffset();
755}
756
757void RfbPlayer::updatePos() {
george823c8fbbf2005-01-24 11:09:08 +0000758 char timePos[30] = "\0";
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000759 long newPos = is->getTimeOffset() / 1000;
george823c8fbbf2005-01-24 11:09:08 +0000760 time_pos_m = newPos / 60;
761 time_pos_s = newPos % 60;
762 if (time_pos_m < 10) {
763 strcat(timePos, "0");
764 _itoa(time_pos_m, timePos+1, 10);
765 } else {
766 _itoa(time_pos_m, timePos, 10);
767 }
768 strcat(timePos, "m:");
769 if (time_pos_s < 10) {
770 strcat(timePos, "0");
771 _itoa(time_pos_s, timePos+strlen(timePos), 10);
772 } else {
773 _itoa(time_pos_s, timePos+strlen(timePos), 10);
774 }
775 strcat(timePos, "s ");
776 strcat(timePos, "(");
777 strcat(timePos, fullSessionTime);
778 strcat(timePos, ")");
779 SetWindowText(timeStatic, timePos);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000780}
781
782void RfbPlayer::skipHandshaking() {
783 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
784 is->skip(skipBytes);
785 state_ = RFBSTATE_NORMAL;
786}
787
788void programInfo() {
789 win32::FileVersionInfo inf;
790 _tprintf(_T("%s - %s, Version %s\n"),
791 inf.getVerString(_T("ProductName")),
792 inf.getVerString(_T("FileDescription")),
793 inf.getVerString(_T("FileVersion")));
794 printf("%s\n", buildTime);
795 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
796}
797
798void programUsage() {
799 printf("usage: rfbplayer <options> <filename>\n");
800 printf("Command-line options:\n");
801 printf(" -help - Provide usage information.\n");
802 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
803 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
804 printf(" -pos <ms> - Sets initial time position in the session file,\n");
805 printf(" in milliseconds. Default: 0.\n");
806 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
807 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
808 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
809}
810
811double playbackSpeed = 1.0;
812long initTime = -1;
813bool autoplay = false;
814bool showControls = true;
815char *fileName;
816bool console = false;
817bool wrong_param = false;
818bool print_usage = false;
819bool acceptBell = false;
820
821bool processParams(int argc, char* argv[]) {
822 for (int i = 1; i < argc; i++) {
823 if ((strcasecmp(argv[i], "-help") == 0) ||
824 (strcasecmp(argv[i], "--help") == 0) ||
825 (strcasecmp(argv[i], "/help") == 0) ||
826 (strcasecmp(argv[i], "-h") == 0) ||
827 (strcasecmp(argv[i], "/h") == 0) ||
828 (strcasecmp(argv[i], "/?") == 0)) {
829 print_usage = true;
830 return true;
831 }
832
833 if ((strcasecmp(argv[i], "-speed") == 0) ||
834 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
835 playbackSpeed = atof(argv[++i]);
836 if (playbackSpeed <= 0) {
837 return false;
838 }
839 continue;
840 }
841
842 if ((strcasecmp(argv[i], "-pos") == 0) ||
843 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
844 initTime = atol(argv[++i]);
845 if (initTime <= 0)
846 return false;
847 continue;
848 }
849
850 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
851 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
852 i++;
853 if (strcasecmp(argv[i], "yes") == 0) {
854 autoplay = true;
855 continue;
856 }
857 if (strcasecmp(argv[i], "no") == 0) {
858 autoplay = false;
859 continue;
860 }
861 return false;
862 }
863
864 if ((strcasecmp(argv[i], "-controls") == 0) ||
865 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
866 i++;
867 if (strcasecmp(argv[i], "yes") == 0) {
868 showControls = true;
869 continue;
870 }
871 if (strcasecmp(argv[i], "no") == 0) {
872 showControls = false;
873 continue;
874 }
875 return false;
876 }
877
878 if ((strcasecmp(argv[i], "-bell") == 0) ||
879 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
880 i++;
881 if (strcasecmp(argv[i], "yes") == 0) {
882 acceptBell = true;
883 continue;
884 }
885 if (strcasecmp(argv[i], "no") == 0) {
886 acceptBell = false;
887 continue;
888 }
889 return false;
890 }
891
892 if (i != argc - 1)
893 return false;
894 }
895
896 fileName = strDup(argv[argc-1]);
897 return true;
898}
899
900//
901// -=- WinMain
902//
903
904int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
905
906 // - Process the command-line
907
908 int argc = __argc;
909 char** argv = __argv;
910 if (argc > 1) {
911 wrong_param = !processParams(argc, argv);
912 console = print_usage | wrong_param;
913 } else {
914 console = true;
915 }
916
917 if (console) {
918 AllocConsole();
919 freopen("CONOUT$","wb",stdout);
920
921 programInfo();
922 if (wrong_param)
923 printf("Wrong a command line.\n");
924 else
925 programUsage();
926
927 printf("\nPress Enter/Return key to continue\n");
928 char c = getch();
929 FreeConsole();
930
931 return 0;
george8267cbcd02005-01-16 15:39:56 +0000932 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000933
934 // Create the player and the thread which reading the rfb data
935 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000936 try {
937 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
938 showControls, acceptBell);
george8210313102005-01-17 13:11:40 +0000939 if (autoplay) player->start();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000940 } catch (rdr::Exception e) {
941 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
942 delete player;
943 return 0;
944 }
945
946 // Run the player
george825bbd61b2004-12-09 17:47:37 +0000947 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000948 MSG msg;
949 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +0000950 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
951 TranslateMessage(&msg);
952 DispatchMessage(&msg);
953 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000954 }
955
956 // Wait while the thread destroying and then destroy the player
957 try{
george8267cbcd02005-01-16 15:39:56 +0000958 while (player->getState() == ThreadStarted) {}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000959 if (player) delete player;
960 } catch (rdr::Exception e) {
961 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
962 }
963
964 return 0;
965};