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