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