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