blob: 2a9827eb402f92e6411a6e6b96ae8cb1b0384f2e [file] [log] [blame]
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001/* 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 Kaplinskyfbfbb922004-11-14 18:28:51 +000021#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
32using namespace rfb;
33using namespace rfb::win32;
34
35// -=- Variables & consts
36
37static LogWriter vlog("RfbPlayer");
38
39TStr rfb::win32::AppName("RfbPlayer");
40extern const char* buildTime;
41
george82e6883de2005-02-08 14:42:12 +000042char wrong_cmd_msg[] =
43 "Wrong command-line parameters!\n"
44 "Use for help: rfbplayer -help";
45
46char 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 Kaplinskyfbfbb922004-11-14 18:28:51 +000057// -=- RfbPlayer's defines
58
59#define strcasecmp _stricmp
george824ea27f62005-01-29 15:03:06 +000060#define MAX_SPEED 10
george82d4d69e62005-02-05 09:23:18 +000061#define MAX_POS_TRACKBAR_RANGE 50
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000062
george82d070c692005-01-19 16:44:04 +000063#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 Kaplinskyfbfbb922004-11-14 18:28:51 +000073//
74// -=- RfbPlayerClass
75
76//
77// Window class used as the basis for RfbPlayer instance
78//
79
80class RfbPlayerClass {
81public:
82 RfbPlayerClass();
83 ~RfbPlayerClass();
84 ATOM classAtom;
85 HINSTANCE instance;
86};
87
88LRESULT 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 Kaplinskyfbfbb922004-11-14 18:28:51 +000095
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
115RfbPlayerClass::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),
george827214b822004-12-12 07:02:51 +0000123 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000124 if (!wndClass.hIcon)
125 printf("unable to load icon:%ld", GetLastError());
126 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
127 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000128 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000129 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
137RfbPlayerClass::~RfbPlayerClass() {
138 if (classAtom) {
139 UnregisterClass((const TCHAR*)classAtom, instance);
140 }
141}
142
143RfbPlayerClass baseClass;
144
145//
146// -=- RfbFrameClass
147
148//
149// Window class used to displaying the rfb data
150//
151
152class RfbFrameClass {
153public:
154 RfbFrameClass();
155 ~RfbFrameClass();
156 ATOM classAtom;
157 HINSTANCE instance;
158};
159
160LRESULT 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
182RfbFrameClass::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
201RfbFrameClass::~RfbFrameClass() {
202 if (classAtom) {
203 UnregisterClass((const TCHAR*)classAtom, instance);
204 }
205}
206
207RfbFrameClass frameClass;
208
209//
210// -=- RfbPlayer instance implementation
211//
212
213RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000214 bool _autoplay = false, bool _acceptBell = false)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000215: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
george82e6883de2005-02-08 14:42:12 +0000216 autoplay(_autoplay), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000217 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000218 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000219 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000220 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000221
george82e6883de2005-02-08 14:42:12 +0000222 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000223
george823c8fbbf2005-01-24 11:09:08 +0000224 // Reset the full session time
225 strcpy(fullSessionTime, "00m:00s");
226
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000227 // Create the main window
228 const TCHAR* name = _T("RfbPlayer");
229 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000230 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000231 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());
george8210313102005-01-17 13:11:40 +0000238 setVisible(true);
george82e6883de2005-02-08 14:42:12 +0000239
george8217e92cb2005-01-31 16:01:02 +0000240 // Open the session file
241 if (fileName) {
242 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000243 if (initTime > 0) setPos(initTime);
244 setSpeed(playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +0000245 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000246}
247
248RfbPlayer::~RfbPlayer() {
249 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000250 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000251 delete rfbReader->join();
252 rfbReader = 0;
253 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000254 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 Kaplinskyfbfbb922004-11-14 18:28:51 +0000264LRESULT
265RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
266 switch (msg) {
267
268 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000269
270 case WM_CREATE:
271 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000272 // 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
george82d070c692005-01-19 16:44:04 +0000277 createToolBar(hwnd);
278
george82006f2792005-02-05 07:40:47 +0000279 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000280
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000281 return 0;
282 }
283
george827214b822004-12-12 07:02:51 +0000284 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000285
286 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000287 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000288 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;
george825c13c662005-01-27 14:48:23 +0000309 case ID_PLAY:
310 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000311 break;
312 case ID_PAUSE:
313 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000314 break;
315 case ID_STOP:
316 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000317 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000318 }
george825c13c662005-01-27 14:48:23 +0000319 break;
320 case ID_PLAYPAUSE:
321 if (isPaused()) {
322 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000323 } else {
324 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000325 }
george825c13c662005-01-27 14:48:23 +0000326 break;
327 case ID_FULLSCREEN:
328 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
329 break;
george8231a36332005-02-06 17:27:34 +0000330 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;
george824ea27f62005-01-29 15:03:06 +0000335 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;
george8201aa6732005-02-06 17:13:03 +0000354 case ID_EXIT:
355 is->resumePlayback();
356 PostQuitMessage(0);
357 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000358 }
359 break;
360
361 // Update frame's window size and add scrollbars if required
362
363 case WM_SIZE:
364 {
george82d070c692005-01-19 16:44:04 +0000365
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000366 Point old_offset = bufferToClient(Point(0, 0));
367
368 // Update the cached sizing information
369 RECT r;
370 GetClientRect(getMainHandle(), &r);
371 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
372 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
373
374 GetWindowRect(getFrameHandle(), &r);
375 window_size = Rect(r.left, r.top, r.right, r.bottom);
376 GetClientRect(getFrameHandle(), &r);
377 client_size = Rect(r.left, r.top, r.right, r.bottom);
378
379 // Determine whether scrollbars are required
380 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000381
382 // Resize the ToolBar
383 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000384
385 // Redraw if required
386 if (!old_offset.equals(bufferToClient(Point(0, 0))))
387 InvalidateRect(getFrameHandle(), 0, TRUE);
388 }
389 break;
george828a471482005-02-06 07:15:53 +0000390
391 // Process messages from posTrackBar
392
393 case WM_HSCROLL:
394 {
395 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
396 Pos *= sliderStepMs;
397
398 switch (LOWORD(wParam)) {
399 case TB_PAGEUP:
400 case TB_PAGEDOWN:
401 case TB_LINEUP:
402 case TB_LINEDOWN:
403 case TB_THUMBTRACK:
404 sliderDraging = true;
405 updatePos(Pos);
406 return 0;
407 case TB_ENDTRACK:
408 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000409 sliderDraging = false;
410 return 0;
411 default:
412 break;
413 }
414 }
415 break;
george829e6e6cc2005-01-29 13:12:05 +0000416
417 case WM_NOTIFY:
418 switch (((NMHDR*)lParam)->code) {
419 case UDN_DELTAPOS:
420 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000421 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000422 char speedStr[20] = "\0";
423 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
424 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
425 double speed;
426
george824ea27f62005-01-29 15:03:06 +0000427 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000428 if (upDown->iDelta > 0) {
429 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
430 } else {
george824ea27f62005-01-29 15:03:06 +0000431 // It's need to round the UpDown position
432 if ((upDown->iPos * 0.5) != getSpeed()) {
433 upDown->iDelta = 0;
434 lResult = TRUE;
435 }
george829e6e6cc2005-01-29 13:12:05 +0000436 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
437 }
438 _gcvt(speed, 5, speedStr);
439 sprintf(speedStr, "%.2f", speed);
440 SetWindowText(speedEdit, speedStr);
441 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000442 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000443 }
george824ea27f62005-01-29 15:03:06 +0000444 }
george829e6e6cc2005-01-29 13:12:05 +0000445 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000446
447 case WM_CLOSE:
448 vlog.debug("WM_CLOSE %x", getMainHandle());
449 PostQuitMessage(0);
450 break;
451 }
452
453 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
454}
455
456LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
457 switch (msg) {
458
459 case WM_PAINT:
460 {
461 if (is->isSeeking()) {
462 seekMode = true;
463 return 0;
464 } else {
465 if (seekMode) {
466 seekMode = false;
467 InvalidateRect(getFrameHandle(), 0, true);
468 UpdateWindow(getFrameHandle());
469 return 0;
470 }
471 }
472
473 PAINTSTRUCT ps;
474 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
475 if (!paintDC)
476 throw SystemException("unable to BeginPaint", GetLastError());
477 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
478
479 if (!pr.is_empty()) {
480
481 if (buffer->bitmap) {
482
483 // Get device context
484 BitmapDC bitmapDC(paintDC, buffer->bitmap);
485
486 // Blit the border if required
487 Rect bufpos = bufferToClient(buffer->getRect());
488 if (!pr.enclosed_by(bufpos)) {
489 vlog.debug("draw border");
490 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
491 RECT r;
492 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
493 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
494 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
495 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
496 }
497
498 // Do the blit
499 Point buf_pos = clientToBuffer(pr.tl);
500 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
501 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
502 throw SystemException("unable to BitBlt to window", GetLastError());
503
504 } else {
505 // Blit a load of black
506 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
507 0, 0, 0, BLACKNESS))
508 throw SystemException("unable to BitBlt to blank window", GetLastError());
509 }
510 }
511 EndPaint(getFrameHandle(), &ps);
512 }
513 return 0;
514
515 case WM_VSCROLL:
516 case WM_HSCROLL:
517 {
518 Point delta;
519 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
520
521 switch (LOWORD(wParam)) {
522 case SB_PAGEUP: newpos -= 50; break;
523 case SB_PAGEDOWN: newpos += 50; break;
524 case SB_LINEUP: newpos -= 5; break;
525 case SB_LINEDOWN: newpos += 5; break;
526 case SB_THUMBTRACK:
527 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
528 default: vlog.info("received unknown scroll message");
529 };
530
531 if (msg == WM_HSCROLL)
532 setViewportOffset(Point(newpos, scrolloffset.y));
533 else
534 setViewportOffset(Point(scrolloffset.x, newpos));
535
536 SCROLLINFO si;
537 si.cbSize = sizeof(si);
538 si.fMask = SIF_POS;
539 si.nPos = newpos;
540 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
541 }
542 break;
543 }
544
545 return DefWindowProc(hwnd, msg, wParam, lParam);
546}
547
548void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000549 bool _autoplay = false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000550 autoplay = _autoplay;
551 playbackSpeed = _playbackSpeed;
552 initTime = _initTime;
553}
554
555void RfbPlayer::applyOptions() {
556 if (initTime >= 0)
557 setPos(initTime);
558 setSpeed(playbackSpeed);
559 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000560}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000561
george82d070c692005-01-19 16:44:04 +0000562void RfbPlayer::createToolBar(HWND parentHwnd) {
563 RECT tRect;
564 InitCommonControls();
565
566 tb.create(ID_TOOLBAR, parentHwnd);
567 tb.addBitmap(4, IDB_TOOLBAR);
568
569 // Create the control buttons
570 tb.addButton(0, ID_PLAY);
571 tb.addButton(1, ID_PAUSE);
572 tb.addButton(2, ID_STOP);
573 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
574 tb.addButton(3, ID_FULLSCREEN);
575 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
576
577 // Create the static control for the time output
578 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
579 tb.getButtonRect(6, &tRect);
580 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
581 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
582 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
583 GetModuleHandle(0), 0);
584 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
585
586 // Create the trackbar control for the time position
587 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
588 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000589 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000590 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
591 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
592 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
593 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000594 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000595 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
596
597 // Create the label with "Speed:" caption
598 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
599 tb.getButtonRect(10, &tRect);
600 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
601 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
602 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
603
604 // Create the edit control and the spin for the speed managing
605 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
606 tb.getButtonRect(11, &tRect);
607 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
608 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
609 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
610 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
611 // It's need to send notify messages to toolbar parent window
612 SetParent(speedEdit, tb.getHandle());
613
614 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
615 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000616 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000617}
618
619void RfbPlayer::setVisible(bool visible) {
620 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
621 if (visible) {
622 // When the window becomes visible, make it active
623 SetForegroundWindow(getMainHandle());
624 SetActiveWindow(getMainHandle());
625 }
626}
627
628void RfbPlayer::setTitle(const char *title) {
629 char _title[256];
630 strcpy(_title, AppName);
631 strcat(_title, " - ");
632 strcat(_title, title);
633 SetWindowText(getMainHandle(), _title);
634}
635
636void RfbPlayer::setFrameSize(int width, int height) {
637 // Calculate and set required size for main window
638 RECT r = {0, 0, width, height};
639 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
640 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
641 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
642 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
643 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
644 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
645
646 // Enable/disable scrollbars as appropriate
647 calculateScrollBars();
648}
649
650void RfbPlayer::calculateScrollBars() {
651 // Calculate the required size of window
652 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
653 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
654 DWORD old_style;
655 RECT r;
656 SetRect(&r, 0, 0, buffer->width(), buffer->height());
657 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
658 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
659
660 // Work out whether scroll bars are required
661 do {
662 old_style = style;
663
664 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
665 style |= WS_HSCROLL;
666 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
667 }
668 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
669 style |= WS_VSCROLL;
670 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
671 }
672 } while (style != old_style);
673
674 // Tell Windows to update the window style & cached settings
675 if (style != current_style) {
676 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
677 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
678 }
679
680 // Update the scroll settings
681 SCROLLINFO si;
682 if (style & WS_VSCROLL) {
683 si.cbSize = sizeof(si);
684 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
685 si.nMin = 0;
686 si.nMax = buffer->height();
687 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
688 maxscrolloffset.y = max(0, si.nMax-si.nPage);
689 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
690 si.nPos = scrolloffset.y;
691 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
692 }
693 if (style & WS_HSCROLL) {
694 si.cbSize = sizeof(si);
695 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
696 si.nMin = 0;
697 si.nMax = buffer->width();
698 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
699 maxscrolloffset.x = max(0, si.nMax-si.nPage);
700 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
701 si.nPos = scrolloffset.x;
702 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
703 }
704}
705
706bool RfbPlayer::setViewportOffset(const Point& tl) {
707/* ***
708 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
709 max(0, min(maxscrolloffset.y, tl.y)));
710 */
711 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
712 max(0, min(tl.y, buffer->height()-client_size.height())));
713 Point delta = np.translate(scrolloffset.negate());
714 if (!np.equals(scrolloffset)) {
715 scrolloffset = np;
716 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
717 UpdateWindow(getFrameHandle());
718 return true;
719 }
720 return false;
721}
722
723void RfbPlayer::close(const char* reason) {
724 setVisible(false);
725 if (reason) {
726 vlog.info("closing - %s", reason);
727 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
728 }
729 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
730}
731
732void RfbPlayer::blankBuffer() {
733 fillRect(buffer->getRect(), 0);
734}
735
736void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000737 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000738 blankBuffer();
739 newSession(fileName);
740 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000741 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000742 if (paused) is->pausePlayback();
743 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000744}
745
746void RfbPlayer::processMsg() {
747 static long update_time = GetTickCount();
748 try {
george828a471482005-02-06 07:15:53 +0000749 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
750 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000751 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000752 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000753 update_time = GetTickCount();
754 }
755 RfbProto::processMsg();
756 } catch (rdr::Exception e) {
757 if (strcmp(e.str(), "[End Of File]") == 0) {
758 rewind();
george8231a36332005-02-06 17:27:34 +0000759 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000760 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000761 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000762 return;
763 }
764 // It's a special exception to perform backward seeking.
765 // We only rewind the stream and seek the offset
766 if (strcmp(e.str(), "[REWIND]") == 0) {
767 long initTime = getSeekOffset();
768 rewind();
769 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000770 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000771 } else {
772 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
773 return;
774 }
775 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000776}
777
778void RfbPlayer::serverInit() {
779 RfbProto::serverInit();
780
781 // Save the server init time for using in setPos()
782 serverInitTime = getTimeOffset() / getSpeed();
783
784 // Resize the backing buffer
785 buffer->setSize(cp.width, cp.height);
786
787 // Check on the true colour mode
788 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000789 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000790
791 // Set the session pixel format
792 buffer->setPF(cp.pf());
793
794 // If the window is not maximised then resize it
795 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
796 setFrameSize(cp.width, cp.height);
797
798 // Set the window title and show it
799 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000800
george82d4d69e62005-02-05 09:23:18 +0000801 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000802 sessionTimeMs = calculateSessionTime(fileName);
803 sprintf(fullSessionTime, "%.2um:%.2us",
804 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000805 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000806 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
807 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000808 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000809
george82006f2792005-02-05 07:40:47 +0000810 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000811}
812
813void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
814 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
815 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
816/* int i;
817 for (i=0;i<count;i++) {
818 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
819 }
820 // *** change to 0, 256?
821 refreshWindowPalette(first, count);
822 palette_changed = true;
823 InvalidateRect(getFrameHandle(), 0, FALSE);*/
824}
825
826void RfbPlayer::bell() {
827 if (acceptBell)
828 MessageBeep(-1);
829}
830
831void RfbPlayer::serverCutText(const char* str, int len) {
832 if (cutText != NULL)
833 delete [] cutText;
834 cutText = new char[len + 1];
835 memcpy(cutText, str, len);
836 cutText[len] = '\0';
837}
838
839void RfbPlayer::frameBufferUpdateEnd() {
840};
841
842void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
843}
844
845void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
846}
847
848
849void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
850 buffer->fillRect(r, pix);
851 invalidateBufferRect(r);
852}
853
854void RfbPlayer::imageRect(const Rect& r, void* pixels) {
855 buffer->imageRect(r, pixels);
856 invalidateBufferRect(r);
857}
858
859void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
860 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
861 invalidateBufferRect(r);
862}
863
864bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
865 Rect rect = bufferToClient(crect);
866 if (rect.intersect(client_size).is_empty()) return false;
867 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
868 InvalidateRect(getFrameHandle(), &invalid, FALSE);
869 return true;
870}
871
george8257f13522005-02-05 08:48:22 +0000872long RfbPlayer::calculateSessionTime(char *filename) {
873 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000874 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000875 try {
876 while (TRUE) {
877 sessionFile.skip(1024);
878 }
879 } catch (rdr::Exception e) {
880 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000881 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000882 } else {
883 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
884 return 0;
885 }
886 }
887 return 0;
888}
889
george8217e92cb2005-01-31 16:01:02 +0000890void RfbPlayer::openSessionFile(char *_fileName) {
891 fileName = strDup(_fileName);
892
893 // Close the previous reading thread
894 if (rfbReader) {
george826e51fcc2005-02-06 13:30:49 +0000895 is->resumePlayback();
george8217e92cb2005-01-31 16:01:02 +0000896 delete rfbReader->join();
897 }
898 blankBuffer();
899 newSession(fileName);
900 setSpeed(playbackSpeed);
901 rfbReader = new rfbSessionReader(this);
902 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000903 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8217e92cb2005-01-31 16:01:02 +0000904}
905
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000906void RfbPlayer::setPaused(bool paused) {
907 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000908 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000909 tb.checkButton(ID_PAUSE, true);
910 tb.checkButton(ID_PLAY, false);
911 tb.checkButton(ID_STOP, false);
912 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
913 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000914 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000915 is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000916 tb.checkButton(ID_PLAY, true);
917 tb.checkButton(ID_STOP, false);
918 tb.checkButton(ID_PAUSE, false);
919 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
920 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000921 }
922}
923
george82006f2792005-02-05 07:40:47 +0000924void RfbPlayer::stopPlayback() {
925 setPos(0);
926 is->pausePlayback();
927 tb.checkButton(ID_STOP, true);
928 tb.checkButton(ID_PLAY, false);
929 tb.checkButton(ID_PAUSE, false);
930 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
931 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000932 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000933}
934
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000935void RfbPlayer::setSpeed(double speed) {
936 serverInitTime = serverInitTime * getSpeed() / speed;
937 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000938 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000939}
940
941double RfbPlayer::getSpeed() {
942 return is->getSpeed();
943}
944
945void RfbPlayer::setPos(long pos) {
946 is->setTimeOffset(max(pos, serverInitTime));
947}
948
949long RfbPlayer::getSeekOffset() {
950 return is->getSeekOffset();
951}
952
953bool RfbPlayer::isSeeking() {
954 return is->isSeeking();
955}
956
957bool RfbPlayer::isSeekMode() {
958 return seekMode;
959}
960
961bool RfbPlayer::isPaused() {
962 return is->isPaused();
963}
964
965long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +0000966 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000967}
968
george828a471482005-02-06 07:15:53 +0000969void RfbPlayer::updatePos(long newPos) {
970 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +0000971 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +0000972 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +0000973 newPos /= 1000;
george8244325492005-02-06 07:29:51 +0000974 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +0000975 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +0000976
977 // Update the position of slider
978 if (!sliderDraging) {
979 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +0000980 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
981 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +0000982 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000983}
984
985void RfbPlayer::skipHandshaking() {
986 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
987 is->skip(skipBytes);
988 state_ = RFBSTATE_NORMAL;
989}
990
991void programInfo() {
992 win32::FileVersionInfo inf;
993 _tprintf(_T("%s - %s, Version %s\n"),
994 inf.getVerString(_T("ProductName")),
995 inf.getVerString(_T("FileDescription")),
996 inf.getVerString(_T("FileVersion")));
997 printf("%s\n", buildTime);
998 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
999}
1000
1001void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001002 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001003}
1004
1005double playbackSpeed = 1.0;
1006long initTime = -1;
1007bool autoplay = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001008char *fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001009bool print_usage = false;
1010bool acceptBell = false;
1011
1012bool processParams(int argc, char* argv[]) {
1013 for (int i = 1; i < argc; i++) {
1014 if ((strcasecmp(argv[i], "-help") == 0) ||
1015 (strcasecmp(argv[i], "--help") == 0) ||
1016 (strcasecmp(argv[i], "/help") == 0) ||
1017 (strcasecmp(argv[i], "-h") == 0) ||
1018 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001019 (strcasecmp(argv[i], "/?") == 0) ||
1020 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001021 print_usage = true;
1022 return true;
1023 }
1024
1025 if ((strcasecmp(argv[i], "-speed") == 0) ||
1026 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1027 playbackSpeed = atof(argv[++i]);
1028 if (playbackSpeed <= 0) {
1029 return false;
1030 }
1031 continue;
1032 }
1033
1034 if ((strcasecmp(argv[i], "-pos") == 0) ||
1035 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1036 initTime = atol(argv[++i]);
1037 if (initTime <= 0)
1038 return false;
1039 continue;
1040 }
1041
1042 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1043 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001044 autoplay = true;
1045 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001046 }
1047
1048 if ((strcasecmp(argv[i], "-bell") == 0) ||
1049 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001050 acceptBell = true;
1051 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001052 }
1053
1054 if (i != argc - 1)
1055 return false;
1056 }
1057
1058 fileName = strDup(argv[argc-1]);
1059 return true;
1060}
1061
1062//
1063// -=- WinMain
1064//
1065
1066int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1067
1068 // - Process the command-line
1069
1070 int argc = __argc;
1071 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001072 if ((argc > 1) && (!processParams(argc, argv))) {
1073 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1074 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001075 }
george82e6883de2005-02-08 14:42:12 +00001076
1077 if (print_usage) {
1078 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001079 return 0;
george8267cbcd02005-01-16 15:39:56 +00001080 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001081
george82e6883de2005-02-08 14:42:12 +00001082 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001083 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001084 try {
1085 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001086 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001087 } catch (rdr::Exception e) {
1088 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1089 delete player;
1090 return 0;
1091 }
1092
1093 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001094 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001095 MSG msg;
1096 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001097 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1098 TranslateMessage(&msg);
1099 DispatchMessage(&msg);
1100 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001101 }
1102
george82e6883de2005-02-08 14:42:12 +00001103 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001104 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001105 if (player) delete player;
1106 } catch (rdr::Exception e) {
1107 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1108 }
1109
1110 return 0;
1111};