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