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