blob: bcdd631542c7c55a93062c385e746e1eed2a3247 [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;
345
346 case WM_CLOSE:
347 vlog.debug("WM_CLOSE %x", getMainHandle());
348 PostQuitMessage(0);
349 break;
350 }
351
352 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
353}
354
355LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
356 switch (msg) {
357
358 case WM_PAINT:
359 {
360 if (is->isSeeking()) {
361 seekMode = true;
362 return 0;
363 } else {
364 if (seekMode) {
365 seekMode = false;
366 InvalidateRect(getFrameHandle(), 0, true);
367 UpdateWindow(getFrameHandle());
368 return 0;
369 }
370 }
371
372 PAINTSTRUCT ps;
373 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
374 if (!paintDC)
375 throw SystemException("unable to BeginPaint", GetLastError());
376 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
377
378 if (!pr.is_empty()) {
379
380 if (buffer->bitmap) {
381
382 // Get device context
383 BitmapDC bitmapDC(paintDC, buffer->bitmap);
384
385 // Blit the border if required
386 Rect bufpos = bufferToClient(buffer->getRect());
387 if (!pr.enclosed_by(bufpos)) {
388 vlog.debug("draw border");
389 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
390 RECT r;
391 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
392 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
393 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
394 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
395 }
396
397 // Do the blit
398 Point buf_pos = clientToBuffer(pr.tl);
399 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
400 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
401 throw SystemException("unable to BitBlt to window", GetLastError());
402
403 } else {
404 // Blit a load of black
405 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
406 0, 0, 0, BLACKNESS))
407 throw SystemException("unable to BitBlt to blank window", GetLastError());
408 }
409 }
410 EndPaint(getFrameHandle(), &ps);
411 }
412 return 0;
413
414 case WM_VSCROLL:
415 case WM_HSCROLL:
416 {
417 Point delta;
418 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
419
420 switch (LOWORD(wParam)) {
421 case SB_PAGEUP: newpos -= 50; break;
422 case SB_PAGEDOWN: newpos += 50; break;
423 case SB_LINEUP: newpos -= 5; break;
424 case SB_LINEDOWN: newpos += 5; break;
425 case SB_THUMBTRACK:
426 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
427 default: vlog.info("received unknown scroll message");
428 };
429
430 if (msg == WM_HSCROLL)
431 setViewportOffset(Point(newpos, scrolloffset.y));
432 else
433 setViewportOffset(Point(scrolloffset.x, newpos));
434
435 SCROLLINFO si;
436 si.cbSize = sizeof(si);
437 si.fMask = SIF_POS;
438 si.nPos = newpos;
439 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
440 }
441 break;
442 }
443
444 return DefWindowProc(hwnd, msg, wParam, lParam);
445}
446
george8267cbcd02005-01-16 15:39:56 +0000447void RfbPlayer::run() {
448 long initTime = -1;
george82cfd81d12005-01-24 11:33:06 +0000449 long update_time = GetTickCount();
george8267cbcd02005-01-16 15:39:56 +0000450
george825c13c662005-01-27 14:48:23 +0000451 // Check the play button if autoplay
452 if (autoplay) {
453 tb.checkButton(ID_PLAY, true);
454 CheckMenuItem(GetMenu(getMainHandle()), ID_PLAYPAUSE, MF_CHECKED);
455 }
456
george8267cbcd02005-01-16 15:39:56 +0000457 // Process the rfb messages
458 while (fRun) {
459 try {
460 if (initTime >= 0) {
461 setPos(initTime);
462 initTime = -1;
463 }
george82cfd81d12005-01-24 11:33:06 +0000464 if ((!isSeeking()) && ((GetTickCount() - update_time) >= 250)) {
george8267cbcd02005-01-16 15:39:56 +0000465 updatePos();
george82cfd81d12005-01-24 11:33:06 +0000466 update_time = GetTickCount();
467 }
george8267cbcd02005-01-16 15:39:56 +0000468 processMsg();
469 } catch (rdr::Exception e) {
470 if (strcmp(e.str(), "[End Of File]") == 0) {
471 rewind();
472 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000473 tb.checkButton(ID_STOP, true);
474 tb.checkButton(ID_PAUSE, false);
475 tb.checkButton(ID_PLAY, false);
george8267cbcd02005-01-16 15:39:56 +0000476 continue;
477 }
478 // It's a special exception to perform backward seeking.
479 // We only rewind the stream and seek the offset
480 if (strcmp(e.str(), "[REWIND]") == 0) {
481 initTime = getSeekOffset();
482 double speed = getSpeed();
483 bool play = !isPaused();
484 rewind();
485 setSpeed(speed);
486 setPaused(!play);
487 } else {
488 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
489 return;
490 }
491 }
492 }
493}
494
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000495void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
496 bool _autoplay = false, bool _showControls = true) {
497 showControls = _showControls;
498 autoplay = _autoplay;
499 playbackSpeed = _playbackSpeed;
500 initTime = _initTime;
501}
502
503void RfbPlayer::applyOptions() {
504 if (initTime >= 0)
505 setPos(initTime);
506 setSpeed(playbackSpeed);
507 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000508}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000509
george82d070c692005-01-19 16:44:04 +0000510void RfbPlayer::createToolBar(HWND parentHwnd) {
511 RECT tRect;
512 InitCommonControls();
513
514 tb.create(ID_TOOLBAR, parentHwnd);
515 tb.addBitmap(4, IDB_TOOLBAR);
516
517 // Create the control buttons
518 tb.addButton(0, ID_PLAY);
519 tb.addButton(1, ID_PAUSE);
520 tb.addButton(2, ID_STOP);
521 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
522 tb.addButton(3, ID_FULLSCREEN);
523 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
524
525 // Create the static control for the time output
526 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
527 tb.getButtonRect(6, &tRect);
528 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
529 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
530 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
531 GetModuleHandle(0), 0);
532 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
533
534 // Create the trackbar control for the time position
535 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
536 tb.getButtonRect(8, &tRect);
537 speedTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
538 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
539 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
540 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
541 // It's need to send notify messages to toolbar parent window
542 SetParent(speedTrackBar, tb.getHandle());
543 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
544
545 // Create the label with "Speed:" caption
546 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
547 tb.getButtonRect(10, &tRect);
548 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
549 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
550 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
551
552 // Create the edit control and the spin for the speed managing
553 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
554 tb.getButtonRect(11, &tRect);
555 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
556 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
557 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
558 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
559 // It's need to send notify messages to toolbar parent window
560 SetParent(speedEdit, tb.getHandle());
561
562 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
563 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
564 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 100, 1, 10);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000565}
566
567void RfbPlayer::setVisible(bool visible) {
568 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
569 if (visible) {
570 // When the window becomes visible, make it active
571 SetForegroundWindow(getMainHandle());
572 SetActiveWindow(getMainHandle());
573 }
574}
575
576void RfbPlayer::setTitle(const char *title) {
577 char _title[256];
578 strcpy(_title, AppName);
579 strcat(_title, " - ");
580 strcat(_title, title);
581 SetWindowText(getMainHandle(), _title);
582}
583
584void RfbPlayer::setFrameSize(int width, int height) {
585 // Calculate and set required size for main window
586 RECT r = {0, 0, width, height};
587 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
588 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
589 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
590 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
591 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
592 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
593
594 // Enable/disable scrollbars as appropriate
595 calculateScrollBars();
596}
597
598void RfbPlayer::calculateScrollBars() {
599 // Calculate the required size of window
600 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
601 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
602 DWORD old_style;
603 RECT r;
604 SetRect(&r, 0, 0, buffer->width(), buffer->height());
605 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
606 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
607
608 // Work out whether scroll bars are required
609 do {
610 old_style = style;
611
612 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
613 style |= WS_HSCROLL;
614 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
615 }
616 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
617 style |= WS_VSCROLL;
618 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
619 }
620 } while (style != old_style);
621
622 // Tell Windows to update the window style & cached settings
623 if (style != current_style) {
624 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
625 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
626 }
627
628 // Update the scroll settings
629 SCROLLINFO si;
630 if (style & WS_VSCROLL) {
631 si.cbSize = sizeof(si);
632 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
633 si.nMin = 0;
634 si.nMax = buffer->height();
635 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
636 maxscrolloffset.y = max(0, si.nMax-si.nPage);
637 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
638 si.nPos = scrolloffset.y;
639 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
640 }
641 if (style & WS_HSCROLL) {
642 si.cbSize = sizeof(si);
643 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
644 si.nMin = 0;
645 si.nMax = buffer->width();
646 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
647 maxscrolloffset.x = max(0, si.nMax-si.nPage);
648 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
649 si.nPos = scrolloffset.x;
650 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
651 }
652}
653
654bool RfbPlayer::setViewportOffset(const Point& tl) {
655/* ***
656 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
657 max(0, min(maxscrolloffset.y, tl.y)));
658 */
659 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
660 max(0, min(tl.y, buffer->height()-client_size.height())));
661 Point delta = np.translate(scrolloffset.negate());
662 if (!np.equals(scrolloffset)) {
663 scrolloffset = np;
664 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
665 UpdateWindow(getFrameHandle());
666 return true;
667 }
668 return false;
669}
670
671void RfbPlayer::close(const char* reason) {
672 setVisible(false);
673 if (reason) {
674 vlog.info("closing - %s", reason);
675 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
676 }
677 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
678}
679
680void RfbPlayer::blankBuffer() {
681 fillRect(buffer->getRect(), 0);
682}
683
684void RfbPlayer::rewind() {
685 blankBuffer();
686 newSession(fileName);
687 skipHandshaking();
688}
689
690void RfbPlayer::serverInit() {
691 RfbProto::serverInit();
692
693 // Save the server init time for using in setPos()
694 serverInitTime = getTimeOffset() / getSpeed();
695
696 // Resize the backing buffer
697 buffer->setSize(cp.width, cp.height);
698
699 // Check on the true colour mode
700 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000701 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000702
703 // Set the session pixel format
704 buffer->setPF(cp.pf());
705
706 // If the window is not maximised then resize it
707 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
708 setFrameSize(cp.width, cp.height);
709
710 // Set the window title and show it
711 setTitle(cp.name());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000712}
713
714void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
715 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
716 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
717/* int i;
718 for (i=0;i<count;i++) {
719 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
720 }
721 // *** change to 0, 256?
722 refreshWindowPalette(first, count);
723 palette_changed = true;
724 InvalidateRect(getFrameHandle(), 0, FALSE);*/
725}
726
727void RfbPlayer::bell() {
728 if (acceptBell)
729 MessageBeep(-1);
730}
731
732void RfbPlayer::serverCutText(const char* str, int len) {
733 if (cutText != NULL)
734 delete [] cutText;
735 cutText = new char[len + 1];
736 memcpy(cutText, str, len);
737 cutText[len] = '\0';
738}
739
740void RfbPlayer::frameBufferUpdateEnd() {
741};
742
743void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
744}
745
746void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
747}
748
749
750void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
751 buffer->fillRect(r, pix);
752 invalidateBufferRect(r);
753}
754
755void RfbPlayer::imageRect(const Rect& r, void* pixels) {
756 buffer->imageRect(r, pixels);
757 invalidateBufferRect(r);
758}
759
760void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
761 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
762 invalidateBufferRect(r);
763}
764
765bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
766 Rect rect = bufferToClient(crect);
767 if (rect.intersect(client_size).is_empty()) return false;
768 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
769 InvalidateRect(getFrameHandle(), &invalid, FALSE);
770 return true;
771}
772
773void RfbPlayer::setPaused(bool paused) {
774 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000775 is->pausePlayback();
776 } else {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000777 is->resumePlayback();
778 }
779}
780
781void RfbPlayer::setSpeed(double speed) {
782 serverInitTime = serverInitTime * getSpeed() / speed;
783 is->setSpeed(speed);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000784}
785
786double RfbPlayer::getSpeed() {
787 return is->getSpeed();
788}
789
790void RfbPlayer::setPos(long pos) {
791 is->setTimeOffset(max(pos, serverInitTime));
792}
793
794long RfbPlayer::getSeekOffset() {
795 return is->getSeekOffset();
796}
797
798bool RfbPlayer::isSeeking() {
799 return is->isSeeking();
800}
801
802bool RfbPlayer::isSeekMode() {
803 return seekMode;
804}
805
806bool RfbPlayer::isPaused() {
807 return is->isPaused();
808}
809
810long RfbPlayer::getTimeOffset() {
811 return is->getTimeOffset();
812}
813
814void RfbPlayer::updatePos() {
george823c8fbbf2005-01-24 11:09:08 +0000815 char timePos[30] = "\0";
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000816 long newPos = is->getTimeOffset() / 1000;
george823c8fbbf2005-01-24 11:09:08 +0000817 time_pos_m = newPos / 60;
818 time_pos_s = newPos % 60;
819 if (time_pos_m < 10) {
820 strcat(timePos, "0");
821 _itoa(time_pos_m, timePos+1, 10);
822 } else {
823 _itoa(time_pos_m, timePos, 10);
824 }
825 strcat(timePos, "m:");
826 if (time_pos_s < 10) {
827 strcat(timePos, "0");
828 _itoa(time_pos_s, timePos+strlen(timePos), 10);
829 } else {
830 _itoa(time_pos_s, timePos+strlen(timePos), 10);
831 }
832 strcat(timePos, "s ");
833 strcat(timePos, "(");
834 strcat(timePos, fullSessionTime);
835 strcat(timePos, ")");
836 SetWindowText(timeStatic, timePos);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000837}
838
839void RfbPlayer::skipHandshaking() {
840 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
841 is->skip(skipBytes);
842 state_ = RFBSTATE_NORMAL;
843}
844
845void programInfo() {
846 win32::FileVersionInfo inf;
847 _tprintf(_T("%s - %s, Version %s\n"),
848 inf.getVerString(_T("ProductName")),
849 inf.getVerString(_T("FileDescription")),
850 inf.getVerString(_T("FileVersion")));
851 printf("%s\n", buildTime);
852 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
853}
854
855void programUsage() {
856 printf("usage: rfbplayer <options> <filename>\n");
857 printf("Command-line options:\n");
858 printf(" -help - Provide usage information.\n");
859 printf(" -speed <value> - Sets playback speed, where 1 is normal speed,\n");
860 printf(" 2 is double speed, 0.5 is half speed. Default: 1.0.\n");
861 printf(" -pos <ms> - Sets initial time position in the session file,\n");
862 printf(" in milliseconds. Default: 0.\n");
863 printf(" -autoplay <yes|no> - Runs the player in the playback mode. Default: \"no\".\n");
864 printf(" -controls <yes|no> - Shows the control panel at the top. Default: \"yes\".\n");
865 printf(" -bell <yes|no> - Accepts the bell. Default: \"no\".\n");
866}
867
868double playbackSpeed = 1.0;
869long initTime = -1;
870bool autoplay = false;
871bool showControls = true;
872char *fileName;
873bool console = false;
874bool wrong_param = false;
875bool print_usage = false;
876bool acceptBell = false;
877
878bool processParams(int argc, char* argv[]) {
879 for (int i = 1; i < argc; i++) {
880 if ((strcasecmp(argv[i], "-help") == 0) ||
881 (strcasecmp(argv[i], "--help") == 0) ||
882 (strcasecmp(argv[i], "/help") == 0) ||
883 (strcasecmp(argv[i], "-h") == 0) ||
884 (strcasecmp(argv[i], "/h") == 0) ||
885 (strcasecmp(argv[i], "/?") == 0)) {
886 print_usage = true;
887 return true;
888 }
889
890 if ((strcasecmp(argv[i], "-speed") == 0) ||
891 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
892 playbackSpeed = atof(argv[++i]);
893 if (playbackSpeed <= 0) {
894 return false;
895 }
896 continue;
897 }
898
899 if ((strcasecmp(argv[i], "-pos") == 0) ||
900 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
901 initTime = atol(argv[++i]);
902 if (initTime <= 0)
903 return false;
904 continue;
905 }
906
907 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
908 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
909 i++;
910 if (strcasecmp(argv[i], "yes") == 0) {
911 autoplay = true;
912 continue;
913 }
914 if (strcasecmp(argv[i], "no") == 0) {
915 autoplay = false;
916 continue;
917 }
918 return false;
919 }
920
921 if ((strcasecmp(argv[i], "-controls") == 0) ||
922 (strcasecmp(argv[i], "/controls") == 0) && (i < argc-1)) {
923 i++;
924 if (strcasecmp(argv[i], "yes") == 0) {
925 showControls = true;
926 continue;
927 }
928 if (strcasecmp(argv[i], "no") == 0) {
929 showControls = false;
930 continue;
931 }
932 return false;
933 }
934
935 if ((strcasecmp(argv[i], "-bell") == 0) ||
936 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
937 i++;
938 if (strcasecmp(argv[i], "yes") == 0) {
939 acceptBell = true;
940 continue;
941 }
942 if (strcasecmp(argv[i], "no") == 0) {
943 acceptBell = false;
944 continue;
945 }
946 return false;
947 }
948
949 if (i != argc - 1)
950 return false;
951 }
952
953 fileName = strDup(argv[argc-1]);
954 return true;
955}
956
957//
958// -=- WinMain
959//
960
961int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
962
963 // - Process the command-line
964
965 int argc = __argc;
966 char** argv = __argv;
967 if (argc > 1) {
968 wrong_param = !processParams(argc, argv);
969 console = print_usage | wrong_param;
970 } else {
971 console = true;
972 }
973
974 if (console) {
975 AllocConsole();
976 freopen("CONOUT$","wb",stdout);
977
978 programInfo();
979 if (wrong_param)
980 printf("Wrong a command line.\n");
981 else
982 programUsage();
983
984 printf("\nPress Enter/Return key to continue\n");
985 char c = getch();
986 FreeConsole();
987
988 return 0;
george8267cbcd02005-01-16 15:39:56 +0000989 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000990
991 // Create the player and the thread which reading the rfb data
992 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000993 try {
994 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
995 showControls, acceptBell);
george8210313102005-01-17 13:11:40 +0000996 if (autoplay) player->start();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000997 } catch (rdr::Exception e) {
998 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
999 delete player;
1000 return 0;
1001 }
1002
1003 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001004 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001005 MSG msg;
1006 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001007 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1008 TranslateMessage(&msg);
1009 DispatchMessage(&msg);
1010 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001011 }
1012
1013 // Wait while the thread destroying and then destroy the player
1014 try{
george8267cbcd02005-01-16 15:39:56 +00001015 while (player->getState() == ThreadStarted) {}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001016 if (player) delete player;
1017 } catch (rdr::Exception e) {
1018 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1019 }
1020
1021 return 0;
1022};