blob: f52561f8f7395cac5bc735f61c7ee1ed6ceb6db9 [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
48#define ID_START_BTN 0
49#define ID_POS_EDT 1
50#define ID_SPEED_EDT 2
51#define ID_POS_TXT 3
52#define ID_SPEED_TXT 4
53#define ID_CLIENT_STC 5
54
55// -=- Custom thread class used to reading the rfb data
56
57class CRfbThread : public Thread {
58 public:
59 CRfbThread(RfbPlayer *_player) {
60 p = _player;
61 setDeleteAfterRun();
62 };
63 ~CRfbThread() {};
64
65 void run() {
66 long initTime = -1;
67
68 // Process the rfb messages
69 while (p->run) {
70 try {
71 if (initTime >= 0) {
72 p->setPos(initTime);
73 initTime = -1;
74 }
75 if (!p->isSeeking())
76 p->updatePos();
77 p->processMsg();
78 } catch (rdr::Exception e) {
79 if (strcmp(e.str(), "[End Of File]") == 0) {
80 p->rewind();
81 p->setPaused(true);
82 continue;
83 }
84 // It's a special exception to perform backward seeking.
85 // We only rewind the stream and seek the offset
86 if (strcmp(e.str(), "[REWIND]") == 0) {
87 initTime = p->getSeekOffset();
88 double speed = p->getSpeed();
89 bool play = !p->isPaused();
90 p->rewind();
91 p->setSpeed(speed);
92 p->setPaused(!play);
93 } else {
94 MessageBox(p->getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
95 return;
96 }
97 }
98 }
99 }
100
101 private:
102 RfbPlayer *p;
103};
104
105//
106// -=- RfbPlayerClass
107
108//
109// Window class used as the basis for RfbPlayer instance
110//
111
112class RfbPlayerClass {
113public:
114 RfbPlayerClass();
115 ~RfbPlayerClass();
116 ATOM classAtom;
117 HINSTANCE instance;
118};
119
120LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
121 LRESULT result;
122
123 if (msg == WM_CREATE)
124 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
125 else if (msg == WM_DESTROY) {
126 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
127 _this->run = false;
128
129 // Resume playback (It's need to quit from FbsInputStream::waitWhilePaused())
130 _this->setPaused(false);
131 SetWindowLong(hwnd, GWL_USERDATA, 0);
132 }
133 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
134 if (!_this) {
135 vlog.info("null _this in %x, message %u", hwnd, msg);
136 return DefWindowProc(hwnd, msg, wParam, lParam);
137 }
138
139 try {
140 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
141 } catch (rdr::Exception& e) {
142 vlog.error("untrapped: %s", e.str());
143 }
144
145 return result;
146};
147
148RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
149 WNDCLASS wndClass;
150 wndClass.style = 0;
151 wndClass.lpfnWndProc = RfbPlayerProc;
152 wndClass.cbClsExtra = 0;
153 wndClass.cbWndExtra = 0;
154 wndClass.hInstance = instance = GetModuleHandle(0);
155 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
156 MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 0, 0, LR_SHARED);
157 if (!wndClass.hIcon)
158 printf("unable to load icon:%ld", GetLastError());
159 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
160 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000161 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000162 wndClass.lpszClassName = _T("RfbPlayerClass");
163 classAtom = RegisterClass(&wndClass);
164 if (!classAtom) {
165 throw rdr::SystemException("unable to register RfbPlayer window class",
166 GetLastError());
167 }
168}
169
170RfbPlayerClass::~RfbPlayerClass() {
171 if (classAtom) {
172 UnregisterClass((const TCHAR*)classAtom, instance);
173 }
174}
175
176RfbPlayerClass baseClass;
177
178//
179// -=- RfbFrameClass
180
181//
182// Window class used to displaying the rfb data
183//
184
185class RfbFrameClass {
186public:
187 RfbFrameClass();
188 ~RfbFrameClass();
189 ATOM classAtom;
190 HINSTANCE instance;
191};
192
193LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
194 LRESULT result;
195
196 if (msg == WM_CREATE)
197 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
198 else if (msg == WM_DESTROY)
199 SetWindowLong(hwnd, GWL_USERDATA, 0);
200 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
201 if (!_this) {
202 vlog.info("null _this in %x, message %u", hwnd, msg);
203 return DefWindowProc(hwnd, msg, wParam, lParam);
204 }
205
206 try {
207 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
208 } catch (rdr::Exception& e) {
209 vlog.error("untrapped: %s", e.str());
210 }
211
212 return result;
213}
214
215RfbFrameClass::RfbFrameClass() : classAtom(0) {
216 WNDCLASS wndClass;
217 wndClass.style = 0;
218 wndClass.lpfnWndProc = FrameProc;
219 wndClass.cbClsExtra = 0;
220 wndClass.cbWndExtra = 0;
221 wndClass.hInstance = instance = GetModuleHandle(0);
222 wndClass.hIcon = 0;
223 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
224 wndClass.hbrBackground = 0;
225 wndClass.lpszMenuName = 0;
226 wndClass.lpszClassName = _T("RfbPlayerClass1");
227 classAtom = RegisterClass(&wndClass);
228 if (!classAtom) {
229 throw rdr::SystemException("unable to register RfbPlayer window class",
230 GetLastError());
231 }
232}
233
234RfbFrameClass::~RfbFrameClass() {
235 if (classAtom) {
236 UnregisterClass((const TCHAR*)classAtom, instance);
237 }
238}
239
240RfbFrameClass frameClass;
241
242//
243// -=- RfbPlayer instance implementation
244//
245
246RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
247 bool _autoplay = false, bool _showControls = true,
248 bool _acceptBell = false)
249: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
250 autoplay(_autoplay), showControls(_showControls), buffer(0), client_size(0, 0, 32, 32),
251 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName), run(true),
252 serverInitTime(0), btnStart(0), txtPos(0), editPos(0), txtSpeed(0), editSpeed(0),
253 lastPos(0), acceptBell(_acceptBell) {
254
255 if (showControls)
256 CTRL_BAR_HEIGHT = 30;
257 else
258 CTRL_BAR_HEIGHT = 0;
259
260 // Create the main window
261 const TCHAR* name = _T("RfbPlayer");
262 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
263 0, 0, 10, 10, 0, 0, baseClass.instance, this);
264 if (!mainHwnd) {
265 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
266 }
267 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
268
269 // Create the backing buffer
270 buffer = new win32::DIBSectionBuffer(getFrameHandle());
271}
272
273RfbPlayer::~RfbPlayer() {
274 vlog.debug("~RfbPlayer");
275 if (mainHwnd) {
276 setVisible(false);
277 DestroyWindow(mainHwnd);
278 mainHwnd = 0;
279 }
280 delete buffer;
281 delete cutText;
282 vlog.debug("~RfbPlayer done");
283}
284
285// RfbPlayer control's tabstop processing
286
287WNDPROC OldProc[3];
288static HWND focusHwnd = 0;
289
290LRESULT CALLBACK TabProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
291 int CONTROL_ID = GetWindowLong(hwnd, GWL_ID);
292
293 if (msg == WM_DESTROY)
294 SetWindowLong(hwnd, GWL_USERDATA, 0);
295
296 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
297 if (!_this)
298 return CallWindowProc(OldProc[CONTROL_ID], hwnd, msg, wParam, lParam);
299
300 switch (msg) {
301 case WM_KEYDOWN:
302
303 // Process tab pressing
304 if (wParam == VK_TAB) {
305 switch (CONTROL_ID){
306 case ID_START_BTN:
307 focusHwnd = _this->getPosEdit();
308 break;
309 case ID_POS_EDT:
310 focusHwnd = _this->getSpeedEdit();
311 break;
312 case ID_SPEED_EDT:
313 focusHwnd = _this->getStartBtn();
314 break;
315 }
316 SetFocus(focusHwnd);
317 }
318
319 // Process return/enter pressing (set the speed and the position)
320 if (wParam == VK_RETURN) {
321 switch (CONTROL_ID){
322
323 // Change the position
324
325 case ID_POS_EDT:
326 {
327 char posTxt[20];
328 long pos;
329 GetWindowText(_this->getPosEdit(), posTxt, 10);
330 pos = atol(posTxt);
331 if (pos != long(_this->getTimeOffset() / 1000) && (pos >= 0))
332 _this->setPos(pos * 1000);
333 else
334 SetWindowText(_this->getPosEdit(), LongToStr(_this->getTimeOffset() / 1000));
335 }
336 break;
337
338 // Change the playback speed
339
340 case ID_SPEED_EDT:
341 {
342 char speedTxt[20];
343 double speed;
344 GetWindowText(_this->getSpeedEdit(), speedTxt, 10);
345 speed = atof(speedTxt);
346 if ((speed != _this->getSpeed()) && (speed != 0))
347 _this->setSpeed(speed);
348 else
349 SetWindowText(_this->getSpeedEdit(), DoubleToStr(_this->getSpeed()));
350 }
351 break;
352 }
353 }
354 break;
355
356 case WM_SETFOCUS:
357 focusHwnd = hwnd;
358 break;
359 }
360
361 return CallWindowProc(OldProc[CONTROL_ID], hwnd, msg, wParam, lParam);
362}
363
364LRESULT
365RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
366 switch (msg) {
367
368 // -=- Process standard window messages
369
370 case WM_SETFOCUS:
371 {
372 if (focusHwnd)
373 SetFocus(focusHwnd);
374 else
375 SetFocus(btnStart);
376 }
377 break;
378
379 case WM_CREATE:
380 {
381 // Create the player controls
382 if (showControls) {
383 btnStart = CreateWindow("BUTTON", "Stop", WS_CHILD | WS_VISIBLE |
384 BS_PUSHBUTTON | BS_NOTIFY, 5, 5, 60, 20, hwnd, (HMENU)ID_START_BTN,
385 baseClass.instance, NULL);
386 txtPos = CreateWindow("STATIC", "Position:", WS_CHILD | WS_VISIBLE,
387 70, 5, 60, 20, hwnd, (HMENU)ID_POS_TXT, baseClass.instance, NULL);
388 editPos = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "0", WS_CHILD |
389 WS_VISIBLE | ES_NUMBER |ES_RIGHT, 135, 5, 60, 20, hwnd,
390 (HMENU)ID_POS_EDT, baseClass.instance, this);
391 txtSpeed = CreateWindow("STATIC", "Speed:", WS_CHILD | WS_VISIBLE,
392 200, 5, 50, 20, hwnd, (HMENU)ID_SPEED_TXT, baseClass.instance, NULL);
393 editSpeed = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "1.0", WS_CHILD |
394 WS_VISIBLE | ES_RIGHT, 255, 5, 60, 20, hwnd, (HMENU)ID_SPEED_EDT,
395 baseClass.instance, this);
396
397 // Windows subclassing (It's used to implement "TabStop")
398 OldProc[0] = (WNDPROC)SetWindowLong(btnStart, GWL_WNDPROC, (LONG)TabProc);
399 OldProc[1] = (WNDPROC)SetWindowLong(editPos, GWL_WNDPROC, (LONG)TabProc);
400 OldProc[2] = (WNDPROC)SetWindowLong(editSpeed, GWL_WNDPROC, (LONG)TabProc);
401
402 SetWindowLong(btnStart, GWL_USERDATA, (long)this);
403 SetWindowLong(editPos, GWL_USERDATA, (long)this);
404 SetWindowLong(editSpeed, GWL_USERDATA, (long)this);
405 }
406
407 // Create the frame window
408 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
409 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
410 hwnd, 0, frameClass.instance, this);
411
412 return 0;
413 }
414
415 // Process the start button messages
416
417 case WM_COMMAND:
418 {
419 if ((LOWORD(wParam) == ID_START_BTN) && (HIWORD(wParam) == BN_CLICKED)) {
420 if (is->isPaused()) {
421 long pos;
422 double speed;
423 char str[20];
424
425 // Change the playback speed
426 GetWindowText(editSpeed, str, 10);
427 speed = atof(str);
428 if ((speed != getSpeed()) && (speed != 0))
429 setSpeed(speed);
430
431 // Change the position
432 GetWindowText(editPos, str, 10);
433 pos = atol(str);
434 if (pos != long(getTimeOffset() / 1000))
435 setPos(pos * 1000);
436 setPaused(false);
437 } else {
438 setPaused(true);
439 updatePos();
440 }
441 }
442 }
443 break;
444
445 // Update frame's window size and add scrollbars if required
446
447 case WM_SIZE:
448 {
449 Point old_offset = bufferToClient(Point(0, 0));
450
451 // Update the cached sizing information
452 RECT r;
453 GetClientRect(getMainHandle(), &r);
454 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
455 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
456
457 GetWindowRect(getFrameHandle(), &r);
458 window_size = Rect(r.left, r.top, r.right, r.bottom);
459 GetClientRect(getFrameHandle(), &r);
460 client_size = Rect(r.left, r.top, r.right, r.bottom);
461
462 // Determine whether scrollbars are required
463 calculateScrollBars();
464
465 // Redraw if required
466 if (!old_offset.equals(bufferToClient(Point(0, 0))))
467 InvalidateRect(getFrameHandle(), 0, TRUE);
468 }
469 break;
470
471 case WM_CLOSE:
472 vlog.debug("WM_CLOSE %x", getMainHandle());
473 PostQuitMessage(0);
474 break;
475 }
476
477 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
478}
479
480LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
481 switch (msg) {
482
483 case WM_PAINT:
484 {
485 if (is->isSeeking()) {
486 seekMode = true;
487 return 0;
488 } else {
489 if (seekMode) {
490 seekMode = false;
491 InvalidateRect(getFrameHandle(), 0, true);
492 UpdateWindow(getFrameHandle());
493 return 0;
494 }
495 }
496
497 PAINTSTRUCT ps;
498 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
499 if (!paintDC)
500 throw SystemException("unable to BeginPaint", GetLastError());
501 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
502
503 if (!pr.is_empty()) {
504
505 if (buffer->bitmap) {
506
507 // Get device context
508 BitmapDC bitmapDC(paintDC, buffer->bitmap);
509
510 // Blit the border if required
511 Rect bufpos = bufferToClient(buffer->getRect());
512 if (!pr.enclosed_by(bufpos)) {
513 vlog.debug("draw border");
514 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
515 RECT r;
516 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
517 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
518 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
519 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
520 }
521
522 // Do the blit
523 Point buf_pos = clientToBuffer(pr.tl);
524 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
525 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
526 throw SystemException("unable to BitBlt to window", GetLastError());
527
528 } else {
529 // Blit a load of black
530 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
531 0, 0, 0, BLACKNESS))
532 throw SystemException("unable to BitBlt to blank window", GetLastError());
533 }
534 }
535 EndPaint(getFrameHandle(), &ps);
536 }
537 return 0;
538
539 case WM_VSCROLL:
540 case WM_HSCROLL:
541 {
542 Point delta;
543 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
544
545 switch (LOWORD(wParam)) {
546 case SB_PAGEUP: newpos -= 50; break;
547 case SB_PAGEDOWN: newpos += 50; break;
548 case SB_LINEUP: newpos -= 5; break;
549 case SB_LINEDOWN: newpos += 5; break;
550 case SB_THUMBTRACK:
551 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
552 default: vlog.info("received unknown scroll message");
553 };
554
555 if (msg == WM_HSCROLL)
556 setViewportOffset(Point(newpos, scrolloffset.y));
557 else
558 setViewportOffset(Point(scrolloffset.x, newpos));
559
560 SCROLLINFO si;
561 si.cbSize = sizeof(si);
562 si.fMask = SIF_POS;
563 si.nPos = newpos;
564 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
565 }
566 break;
567 }
568
569 return DefWindowProc(hwnd, msg, wParam, lParam);
570}
571
572void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
573 bool _autoplay = false, bool _showControls = true) {
574 showControls = _showControls;
575 autoplay = _autoplay;
576 playbackSpeed = _playbackSpeed;
577 initTime = _initTime;
578}
579
580void RfbPlayer::applyOptions() {
581 if (initTime >= 0)
582 setPos(initTime);
583 setSpeed(playbackSpeed);
584 setPaused(!autoplay);
585
586 // Update the position
587 SetWindowText(editPos, LongToStr(initTime / 1000));
588}
589
590void RfbPlayer::setVisible(bool visible) {
591 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
592 if (visible) {
593 // When the window becomes visible, make it active
594 SetForegroundWindow(getMainHandle());
595 SetActiveWindow(getMainHandle());
596 }
597}
598
599void RfbPlayer::setTitle(const char *title) {
600 char _title[256];
601 strcpy(_title, AppName);
602 strcat(_title, " - ");
603 strcat(_title, title);
604 SetWindowText(getMainHandle(), _title);
605}
606
607void RfbPlayer::setFrameSize(int width, int height) {
608 // Calculate and set required size for main window
609 RECT r = {0, 0, width, height};
610 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
611 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
612 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
613 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
614 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
615 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
616
617 // Enable/disable scrollbars as appropriate
618 calculateScrollBars();
619}
620
621void RfbPlayer::calculateScrollBars() {
622 // Calculate the required size of window
623 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
624 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
625 DWORD old_style;
626 RECT r;
627 SetRect(&r, 0, 0, buffer->width(), buffer->height());
628 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
629 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
630
631 // Work out whether scroll bars are required
632 do {
633 old_style = style;
634
635 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
636 style |= WS_HSCROLL;
637 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
638 }
639 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
640 style |= WS_VSCROLL;
641 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
642 }
643 } while (style != old_style);
644
645 // Tell Windows to update the window style & cached settings
646 if (style != current_style) {
647 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
648 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
649 }
650
651 // Update the scroll settings
652 SCROLLINFO si;
653 if (style & WS_VSCROLL) {
654 si.cbSize = sizeof(si);
655 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
656 si.nMin = 0;
657 si.nMax = buffer->height();
658 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
659 maxscrolloffset.y = max(0, si.nMax-si.nPage);
660 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
661 si.nPos = scrolloffset.y;
662 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
663 }
664 if (style & WS_HSCROLL) {
665 si.cbSize = sizeof(si);
666 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
667 si.nMin = 0;
668 si.nMax = buffer->width();
669 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
670 maxscrolloffset.x = max(0, si.nMax-si.nPage);
671 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
672 si.nPos = scrolloffset.x;
673 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
674 }
675}
676
677bool RfbPlayer::setViewportOffset(const Point& tl) {
678/* ***
679 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
680 max(0, min(maxscrolloffset.y, tl.y)));
681 */
682 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
683 max(0, min(tl.y, buffer->height()-client_size.height())));
684 Point delta = np.translate(scrolloffset.negate());
685 if (!np.equals(scrolloffset)) {
686 scrolloffset = np;
687 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
688 UpdateWindow(getFrameHandle());
689 return true;
690 }
691 return false;
692}
693
694void RfbPlayer::close(const char* reason) {
695 setVisible(false);
696 if (reason) {
697 vlog.info("closing - %s", reason);
698 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
699 }
700 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
701}
702
703void RfbPlayer::blankBuffer() {
704 fillRect(buffer->getRect(), 0);
705}
706
707void RfbPlayer::rewind() {
708 blankBuffer();
709 newSession(fileName);
710 skipHandshaking();
711}
712
713void RfbPlayer::serverInit() {
714 RfbProto::serverInit();
715
716 // Save the server init time for using in setPos()
717 serverInitTime = getTimeOffset() / getSpeed();
718
719 // Resize the backing buffer
720 buffer->setSize(cp.width, cp.height);
721
722 // Check on the true colour mode
723 if (!(cp.pf()).trueColour)
724 throw rdr::Exception("This version plays only true colour session!");
725
726 // Set the session pixel format
727 buffer->setPF(cp.pf());
728
729 // If the window is not maximised then resize it
730 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
731 setFrameSize(cp.width, cp.height);
732
733 // Set the window title and show it
734 setTitle(cp.name());
735 setVisible(true);
736
737 // Set the player's param
738 applyOptions();
739}
740
741void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
742 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
743 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
744/* int i;
745 for (i=0;i<count;i++) {
746 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
747 }
748 // *** change to 0, 256?
749 refreshWindowPalette(first, count);
750 palette_changed = true;
751 InvalidateRect(getFrameHandle(), 0, FALSE);*/
752}
753
754void RfbPlayer::bell() {
755 if (acceptBell)
756 MessageBeep(-1);
757}
758
759void RfbPlayer::serverCutText(const char* str, int len) {
760 if (cutText != NULL)
761 delete [] cutText;
762 cutText = new char[len + 1];
763 memcpy(cutText, str, len);
764 cutText[len] = '\0';
765}
766
767void RfbPlayer::frameBufferUpdateEnd() {
768};
769
770void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
771}
772
773void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
774}
775
776
777void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
778 buffer->fillRect(r, pix);
779 invalidateBufferRect(r);
780}
781
782void RfbPlayer::imageRect(const Rect& r, void* pixels) {
783 buffer->imageRect(r, pixels);
784 invalidateBufferRect(r);
785}
786
787void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
788 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
789 invalidateBufferRect(r);
790}
791
792bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
793 Rect rect = bufferToClient(crect);
794 if (rect.intersect(client_size).is_empty()) return false;
795 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
796 InvalidateRect(getFrameHandle(), &invalid, FALSE);
797 return true;
798}
799
800void RfbPlayer::setPaused(bool paused) {
801 if (paused) {
802 if (btnStart) SetWindowText(btnStart, "Start");
803 is->pausePlayback();
804 } else {
805 if (btnStart) SetWindowText(btnStart, "Stop");
806 is->resumePlayback();
807 }
808}
809
810void RfbPlayer::setSpeed(double speed) {
811 serverInitTime = serverInitTime * getSpeed() / speed;
812 is->setSpeed(speed);
813 if (editSpeed)
814 SetWindowText(editSpeed, DoubleToStr(speed, 1));
815}
816
817double RfbPlayer::getSpeed() {
818 return is->getSpeed();
819}
820
821void RfbPlayer::setPos(long pos) {
822 is->setTimeOffset(max(pos, serverInitTime));
823}
824
825long RfbPlayer::getSeekOffset() {
826 return is->getSeekOffset();
827}
828
829bool RfbPlayer::isSeeking() {
830 return is->isSeeking();
831}
832
833bool RfbPlayer::isSeekMode() {
834 return seekMode;
835}
836
837bool RfbPlayer::isPaused() {
838 return is->isPaused();
839}
840
841long RfbPlayer::getTimeOffset() {
842 return is->getTimeOffset();
843}
844
845void RfbPlayer::updatePos() {
846 long newPos = is->getTimeOffset() / 1000;
847 if (editPos && lastPos != newPos) {
848 lastPos = newPos;
849 SetWindowText(editPos, LongToStr(lastPos));
850 }
851}
852
853void RfbPlayer::skipHandshaking() {
854 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
855 is->skip(skipBytes);
856 state_ = RFBSTATE_NORMAL;
857}
858
859void programInfo() {
860 win32::FileVersionInfo inf;
861 _tprintf(_T("%s - %s, Version %s\n"),
862 inf.getVerString(_T("ProductName")),
863 inf.getVerString(_T("FileDescription")),
864 inf.getVerString(_T("FileVersion")));
865 printf("%s\n", buildTime);
866 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
867}
868
869void programUsage() {
870 printf("usage: rfbplayer <options> <filename>\n");
871 printf("Command-line options:\n");
872 printf(" -help - Provide usage information.\n");
873 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
874 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
875 printf(" -pos <ms> - Sets initial time position in the session file,\n");
876 printf(" in milliseconds. Default: 0.\n");
877 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
878 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
879 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
880}
881
882double playbackSpeed = 1.0;
883long initTime = -1;
884bool autoplay = false;
885bool showControls = true;
886char *fileName;
887bool console = false;
888bool wrong_param = false;
889bool print_usage = false;
890bool acceptBell = false;
891
892bool processParams(int argc, char* argv[]) {
893 for (int i = 1; i < argc; i++) {
894 if ((strcasecmp(argv[i], "-help") == 0) ||
895 (strcasecmp(argv[i], "--help") == 0) ||
896 (strcasecmp(argv[i], "/help") == 0) ||
897 (strcasecmp(argv[i], "-h") == 0) ||
898 (strcasecmp(argv[i], "/h") == 0) ||
899 (strcasecmp(argv[i], "/?") == 0)) {
900 print_usage = true;
901 return true;
902 }
903
904 if ((strcasecmp(argv[i], "-speed") == 0) ||
905 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
906 playbackSpeed = atof(argv[++i]);
907 if (playbackSpeed <= 0) {
908 return false;
909 }
910 continue;
911 }
912
913 if ((strcasecmp(argv[i], "-pos") == 0) ||
914 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
915 initTime = atol(argv[++i]);
916 if (initTime <= 0)
917 return false;
918 continue;
919 }
920
921 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
922 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
923 i++;
924 if (strcasecmp(argv[i], "yes") == 0) {
925 autoplay = true;
926 continue;
927 }
928 if (strcasecmp(argv[i], "no") == 0) {
929 autoplay = false;
930 continue;
931 }
932 return false;
933 }
934
935 if ((strcasecmp(argv[i], "-controls") == 0) ||
936 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
937 i++;
938 if (strcasecmp(argv[i], "yes") == 0) {
939 showControls = true;
940 continue;
941 }
942 if (strcasecmp(argv[i], "no") == 0) {
943 showControls = false;
944 continue;
945 }
946 return false;
947 }
948
949 if ((strcasecmp(argv[i], "-bell") == 0) ||
950 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
951 i++;
952 if (strcasecmp(argv[i], "yes") == 0) {
953 acceptBell = true;
954 continue;
955 }
956 if (strcasecmp(argv[i], "no") == 0) {
957 acceptBell = false;
958 continue;
959 }
960 return false;
961 }
962
963 if (i != argc - 1)
964 return false;
965 }
966
967 fileName = strDup(argv[argc-1]);
968 return true;
969}
970
971//
972// -=- WinMain
973//
974
975int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
976
977 // - Process the command-line
978
979 int argc = __argc;
980 char** argv = __argv;
981 if (argc > 1) {
982 wrong_param = !processParams(argc, argv);
983 console = print_usage | wrong_param;
984 } else {
985 console = true;
986 }
987
988 if (console) {
989 AllocConsole();
990 freopen("CONOUT$","wb",stdout);
991
992 programInfo();
993 if (wrong_param)
994 printf("Wrong a command line.\n");
995 else
996 programUsage();
997
998 printf("\nPress Enter/Return key to continue\n");
999 char c = getch();
1000 FreeConsole();
1001
1002 return 0;
1003 }
1004
1005 // Create the player and the thread which reading the rfb data
1006 RfbPlayer *player = NULL;
1007 CRfbThread *rfbThread = NULL;
1008 try {
1009 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
1010 showControls, acceptBell);
1011 rfbThread = new CRfbThread(player);
1012 rfbThread->start();
1013 } catch (rdr::Exception e) {
1014 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1015 delete player;
1016 return 0;
1017 }
1018
1019 // Run the player
1020 MSG msg;
1021 while (GetMessage(&msg, NULL, 0, 0) > 0) {
1022 TranslateMessage(&msg);
1023 DispatchMessage(&msg);
1024 }
1025
1026 // Wait while the thread destroying and then destroy the player
1027 try{
1028 while (rfbThread->getState() == ThreadStarted) {}
1029 if (player) delete player;
1030 } catch (rdr::Exception e) {
1031 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1032 }
1033
1034 return 0;
1035};