|
1 |
| -#include "ScreenCapture.h" |
2 | 1 | #include "DXFrameProcessor.h"
|
3 | 2 | #include "GDIFrameProcessor.h"
|
4 | 3 | #include "GDIMouseProcessor.h"
|
5 | 4 | #include "GDIWindowProcessor.h"
|
| 5 | +#include "ScreenCapture.h" |
6 | 6 | #include "ThreadManager.h"
|
7 | 7 |
|
8 | 8 | #define NOMINMAX
|
9 | 9 | #define WIN32_LEAN_AND_MEAN
|
10 | 10 | #include <windows.h>
|
11 | 11 |
|
| 12 | +#include <iostream> |
12 | 13 | #include <memory>
|
13 | 14 | #include <string>
|
14 |
| -#include <iostream> |
15 | 15 |
|
16 | 16 | namespace SL {
|
17 |
| - namespace Screen_Capture { |
18 |
| - |
| 17 | +namespace Screen_Capture { |
19 | 18 |
|
20 |
| - template<class T>void ProcessExit(DUPL_RETURN Ret, T* TData) { |
21 |
| - if (Ret != DUPL_RETURN_SUCCESS) |
22 |
| - { |
23 |
| - if (Ret == DUPL_RETURN_ERROR_EXPECTED) |
24 |
| - { |
25 |
| - // The system is in a transition state so request the duplication be restarted |
26 |
| - TData->CommonData_.ExpectedErrorEvent = true; |
27 |
| - } |
28 |
| - else |
29 |
| - { |
30 |
| - // Unexpected error so exit the application |
31 |
| - TData->CommonData_.UnexpectedErrorEvent = true; |
32 |
| - } |
| 19 | + template <class T> void ProcessExit(DUPL_RETURN Ret, T *TData) |
| 20 | + { |
| 21 | + if (Ret != DUPL_RETURN_SUCCESS) { |
| 22 | + if (Ret == DUPL_RETURN_ERROR_EXPECTED) { |
| 23 | + // The system is in a transition state so request the duplication be restarted |
| 24 | + TData->CommonData_.ExpectedErrorEvent = true; |
33 | 25 | }
|
34 |
| - } |
35 |
| - template<class T>bool SwitchToInputDesktop(const std::shared_ptr<T> data) { |
36 |
| - HDESK CurrentDesktop = nullptr; |
37 |
| - CurrentDesktop = OpenInputDesktop(0, FALSE, GENERIC_ALL); |
38 |
| - if (!CurrentDesktop) |
39 |
| - { |
40 |
| - // We do not have access to the desktop so request a retry |
41 |
| - data->CommonData_.ExpectedErrorEvent = true; |
42 |
| - ProcessExit(DUPL_RETURN::DUPL_RETURN_ERROR_EXPECTED, data.get()); |
43 |
| - return false; |
| 26 | + else { |
| 27 | + // Unexpected error so exit the application |
| 28 | + TData->CommonData_.UnexpectedErrorEvent = true; |
44 | 29 | }
|
| 30 | + } |
| 31 | + } |
| 32 | + template <class T> bool SwitchToInputDesktop(const std::shared_ptr<T> data) |
| 33 | + { |
| 34 | + HDESK CurrentDesktop = nullptr; |
| 35 | + CurrentDesktop = OpenInputDesktop(0, FALSE, GENERIC_ALL); |
| 36 | + if (!CurrentDesktop) { |
| 37 | + // We do not have access to the desktop so request a retry |
| 38 | + data->CommonData_.ExpectedErrorEvent = true; |
| 39 | + ProcessExit(DUPL_RETURN::DUPL_RETURN_ERROR_EXPECTED, data.get()); |
| 40 | + return false; |
| 41 | + } |
45 | 42 |
|
46 |
| - // Attach desktop to this thread |
47 |
| - bool DesktopAttached = SetThreadDesktop(CurrentDesktop) != 0; |
48 |
| - CloseDesktop(CurrentDesktop); |
49 |
| - CurrentDesktop = nullptr; |
50 |
| - if (!DesktopAttached) |
51 |
| - { |
52 |
| - // We do not have access to the desktop so request a retry |
53 |
| - data->CommonData_.ExpectedErrorEvent = true; |
54 |
| - ProcessExit(DUPL_RETURN::DUPL_RETURN_ERROR_EXPECTED, data.get()); |
55 |
| - return false; |
56 |
| - } |
57 |
| - return true; |
| 43 | + // Attach desktop to this thread |
| 44 | + bool DesktopAttached = SetThreadDesktop(CurrentDesktop) != 0; |
| 45 | + CloseDesktop(CurrentDesktop); |
| 46 | + CurrentDesktop = nullptr; |
| 47 | + if (!DesktopAttached) { |
| 48 | + // We do not have access to the desktop so request a retry |
| 49 | + data->CommonData_.ExpectedErrorEvent = true; |
| 50 | + ProcessExit(DUPL_RETURN::DUPL_RETURN_ERROR_EXPECTED, data.get()); |
| 51 | + return false; |
58 | 52 | }
|
59 |
| - void RunCaptureMouse(std::shared_ptr<Thread_Data> data) { |
60 |
| - if (!SwitchToInputDesktop(data)) return; |
61 |
| - TryCaptureMouse<GDIMouseProcessor>(data); |
62 |
| - } |
63 |
| - void RunCaptureMonitor(std::shared_ptr<Thread_Data> data, Monitor monitor) { |
64 |
| - //need to switch to the input desktop for capturing... |
65 |
| - if (!SwitchToInputDesktop(data)) return; |
| 53 | + return true; |
| 54 | + } |
| 55 | + void RunCaptureMouse(std::shared_ptr<Thread_Data> data) |
| 56 | + { |
| 57 | + if (!SwitchToInputDesktop(data)) |
| 58 | + return; |
| 59 | + TryCaptureMouse<GDIMouseProcessor>(data); |
| 60 | + } |
| 61 | + void RunCaptureMonitor(std::shared_ptr<Thread_Data> data, Monitor monitor) |
| 62 | + { |
| 63 | + // need to switch to the input desktop for capturing... |
| 64 | + if (!SwitchToInputDesktop(data)) |
| 65 | + return; |
66 | 66 | #if defined _DEBUG || !defined NDEBUG
|
67 |
| - std::cout << "Starting to Capture on Monitor " << Name(monitor) << std::endl; |
68 |
| - std::cout << "Trying DirectX Desktop Duplication " << std::endl; |
| 67 | + std::cout << "Starting to Capture on Monitor " << Name(monitor) << std::endl; |
| 68 | + std::cout << "Trying DirectX Desktop Duplication " << std::endl; |
69 | 69 | #endif
|
70 |
| - if (monitor.Adapter == -1) |
71 |
| - { |
72 |
| - TryCaptureMonitor<GDIFrameProcessor>(data, monitor); |
73 |
| - } |
74 |
| - else |
75 |
| - { |
76 |
| - TryCaptureMonitor<DXFrameProcessor>(data, monitor); |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 |
| - void RunCaptureWindow(std::shared_ptr<Thread_Data> data, Window wnd) { |
81 |
| - //need to switch to the input desktop for capturing... |
82 |
| - if (!SwitchToInputDesktop(data)) return; |
83 |
| - TryCaptureWindow<GDIFrameProcessor>(data, wnd); |
| 70 | + if (!TryCaptureMonitor<DXFrameProcessor>(data, monitor)) { // if DX is not supported, fallback to GDI capture |
| 71 | +#if defined _DEBUG || !defined NDEBUG |
| 72 | + std::cout << "DirectX Desktop Duplication not supported, falling back to GDI Capturing . . ." << std::endl; |
| 73 | +#endif |
| 74 | + TryCaptureMonitor<GDIFrameProcessor>(data, monitor); |
84 | 75 | }
|
85 | 76 | }
|
86 |
| -} |
87 |
| - |
88 |
| - |
89 | 77 |
|
| 78 | + void RunCaptureWindow(std::shared_ptr<Thread_Data> data, Window wnd) |
| 79 | + { |
| 80 | + // need to switch to the input desktop for capturing... |
| 81 | + if (!SwitchToInputDesktop(data)) |
| 82 | + return; |
| 83 | + TryCaptureWindow<GDIFrameProcessor>(data, wnd); |
| 84 | + } |
| 85 | +} // namespace Screen_Capture |
| 86 | +} // namespace SL |
0 commit comments