Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rfbplayer.cxx 25KB

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