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