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