Skip to content

Commit

Permalink
Develop (#15)
Browse files Browse the repository at this point in the history
cge:
* cge SDL scancode map to Windows
+ --disable-keys option

tools:
+ cgvhid_reset
* support SDL 2.0.12 and 2.0.14
  • Loading branch information
UMU618 authored Mar 30, 2021
1 parent 70f84e4 commit 732bf07
Show file tree
Hide file tree
Showing 14 changed files with 538 additions and 31 deletions.
24 changes: 23 additions & 1 deletion src/cge/cge/cge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "pch.h"

#include <boost/algorithm/string.hpp>
#include <boost/asio/detail/winsock_init.hpp>
#include <boost/program_options.hpp>

Expand Down Expand Up @@ -120,12 +121,14 @@ int main(int argc, char* argv[]) {
int video_gop = 0;
std::string video_preset;
uint32_t video_quality = 0;
std::vector<uint8_t> disable_keys;

try {
std::string keyboard_replay_string;
std::string gamepad_replay_string;
std::string video_codec;
std::string hardware_encoder_string;
std::string disable_keys_string;

po::options_description desc("Usage");
// clang-format off
Expand All @@ -143,6 +146,9 @@ int main(int argc, char* argv[]) {
("control-port",
po::value<uint16_t>(&control_port)->default_value(kDefaultControlPort),
"Set the UDP port for control flow")
("disable-keys",
po::value<std::string>(&disable_keys_string),
"Disable scan codes. eg: 226,230 disable ALT; 227,231 disable WIN")
("donot-present",
po::value<bool>(&donot_present)->default_value(kDefaultDonotPresent),
"Tell cgh don't present")
Expand Down Expand Up @@ -219,6 +225,21 @@ int main(int argc, char* argv[]) {
keyboard_replay = static_cast<KeyboardReplay>(std::distance(
kValidKeyboardReplayMethods.cbegin(), keyboard_replay_pos));

if (!disable_keys_string.empty()) {
std::vector<std::string> arr;
boost::algorithm::split(arr, disable_keys_string, boost::is_any_of(","),
boost::algorithm::token_compress_on);
disable_keys.reserve(arr.size());
for (auto& e : arr) {
if (!e.empty()) {
int key = std::atoi(e.data());
if (0 < key && key < 256) {
disable_keys.push_back(key);
}
}
}
}

auto gamepad_replay_pos =
std::find(kValidGamepadReplayMethods.cbegin(),
kValidGamepadReplayMethods.cend(), gamepad_replay_string);
Expand Down Expand Up @@ -299,6 +320,7 @@ int main(int argc, char* argv[]) {
<< "audio-codec: " << audio_codec << '\n'
<< "bind-address: " << bind_address << '\n'
<< "control-port: " << control_port << '\n'
<< "disable-keys: " << disable_keys_string << '\n'
<< "donot-present: " << std::boolalpha << donot_present << '\n'
<< "hardware-encoder: " << hardware_encoder_string << '\n'
<< "keyboard-replay: " << keyboard_replay_string << '\n'
Expand Down Expand Up @@ -347,7 +369,7 @@ int main(int argc, char* argv[]) {
});
Engine::GetInstance().Run(tcp::endpoint(kAddress, stream_port),
udp::endpoint(kAddress, control_port),
std::move(audio_codec), audio_bitrate,
std::move(audio_codec), audio_bitrate, disable_keys,
keyboard_replay, gamepad_replay, video_bitrate,
video_codec_id, hardware_encoder, video_gop,
std::move(video_preset), video_quality);
Expand Down
12 changes: 6 additions & 6 deletions src/cge/cge/cge.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(PlatformTarget)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)tmp\$(PlatformTarget)\$(Configuration)\$(ProjectName)\</IntDir>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(IncludePath)</IncludePath>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(SDL2_ROOT)\include;$(IncludePath)</IncludePath>
<LibraryPath>$(BOOST_ROOT)\stage\lib;$(FFMPEG_ROOT)\lib\$(PlatformTarget);$(LibraryPath)</LibraryPath>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<ClangTidyChecks>
Expand All @@ -114,7 +114,7 @@
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(PlatformTarget)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)tmp\$(PlatformTarget)\$(Configuration)\$(ProjectName)\</IntDir>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(IncludePath)</IncludePath>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(SDL2_ROOT)\include;$(IncludePath)</IncludePath>
<LibraryPath>$(BOOST_ROOT)\stage\lib;$(FFMPEG_ROOT)\lib\$(PlatformTarget);$(LibraryPath)</LibraryPath>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<ClangTidyChecks>
Expand All @@ -126,7 +126,7 @@
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(PlatformTarget)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)tmp\$(PlatformTarget)\$(Configuration)\$(ProjectName)\</IntDir>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(IncludePath)</IncludePath>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(SDL2_ROOT)\include;$(IncludePath)</IncludePath>
<LibraryPath>$(BOOST_ROOT)\stage\lib;$(FFMPEG_ROOT)\lib\$(PlatformTarget);$(LibraryPath)</LibraryPath>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<ClangTidyChecks>
Expand All @@ -138,7 +138,7 @@
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(PlatformTarget)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)tmp\$(PlatformTarget)\$(Configuration)\$(ProjectName)\</IntDir>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(IncludePath)</IncludePath>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(SDL2_ROOT)\include;$(IncludePath)</IncludePath>
<LibraryPath>$(BOOST_ROOT)\stage\lib;$(FFMPEG_ROOT)\lib\$(PlatformTarget);$(LibraryPath)</LibraryPath>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<EnableClangTidyCodeAnalysis>true</EnableClangTidyCodeAnalysis>
Expand All @@ -148,7 +148,7 @@
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(PlatformTarget)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)tmp\$(PlatformTarget)\$(Configuration)\$(ProjectName)\</IntDir>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(IncludePath)</IncludePath>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(SDL2_ROOT)\include;$(IncludePath)</IncludePath>
<LibraryPath>$(BOOST_ROOT)\stage\lib;$(FFMPEG_ROOT)\lib\$(PlatformTarget);$(LibraryPath)</LibraryPath>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<EnableClangTidyCodeAnalysis>true</EnableClangTidyCodeAnalysis>
Expand All @@ -158,7 +158,7 @@
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(PlatformTarget)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)tmp\$(PlatformTarget)\$(Configuration)\$(ProjectName)\</IntDir>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(IncludePath)</IncludePath>
<IncludePath>$(BOOST_ROOT);$(FFMPEG_ROOT)\include;$(SDL2_ROOT)\include;$(IncludePath)</IncludePath>
<LibraryPath>$(BOOST_ROOT)\stage\lib;$(FFMPEG_ROOT)\lib\$(PlatformTarget);$(LibraryPath)</LibraryPath>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<EnableClangTidyCodeAnalysis>true</EnableClangTidyCodeAnalysis>
Expand Down
7 changes: 4 additions & 3 deletions src/cge/cge/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void Engine::Run(tcp::endpoint ws_endpoint,
udp::endpoint udp_endpoint,
std::string audio_codec,
uint64_t audio_bitrate,
std::vector<uint8_t> disable_keys,
KeyboardReplay keyboard_replay,
GamepadReplay gamepad_replay,
uint64_t video_bitrate,
Expand All @@ -54,9 +55,9 @@ void Engine::Run(tcp::endpoint ws_endpoint,
std::cout << "WebSocket server on: " << ws_endpoint << '\n';
ws_server_->Run();
}
udp_server_ = std::make_shared<UdpServer>(*this, udp_endpoint,
std::move(keyboard_replay),
std::move(gamepad_replay));
udp_server_ = std::make_shared<UdpServer>(
*this, udp_endpoint, std::move(disable_keys),
std::move(keyboard_replay), std::move(gamepad_replay));
std::cout << "UDP Server on: " << udp_endpoint << '\n';
udp_server_->Run();
} catch (std::exception& e) {
Expand Down
1 change: 1 addition & 0 deletions src/cge/cge/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Engine {
udp::endpoint udp_endpoint,
std::string audio_codec,
uint64_t audio_bitrate,
std::vector<uint8_t> disable_keys,
KeyboardReplay keyboard_replay,
GamepadReplay gamepad_replay,
uint64_t video_bitrate,
Expand Down
99 changes: 95 additions & 4 deletions src/cge/cge/udp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "vigem_client.h"

#include <SDL2/SDL_scancode.h>

namespace {

inline void Fail(beast::error_code ec, std::string_view what) {
Expand Down Expand Up @@ -114,16 +116,98 @@ uint16_t GamepadButtonMap(uint16_t button) {
}
}

// SDL scancode to Windows scancode
uint8_t ScanCodeMap(uint16_t key_code) {
switch (key_code) {
case SDL_SCANCODE_MODE:
return CgvhidClient::VkToScancode(VK_MODECHANGE);
case SDL_SCANCODE_SELECT:
return CgvhidClient::VkToScancode(VK_SELECT);
case SDL_SCANCODE_EXECUTE:
return CgvhidClient::VkToScancode(VK_EXECUTE);
case SDL_SCANCODE_HELP:
return CgvhidClient::VkToScancode(VK_HELP);
case SDL_SCANCODE_PAUSE:
return CgvhidClient::VkToScancode(VK_PAUSE);
case SDL_SCANCODE_NUMLOCKCLEAR:
return CgvhidClient::VkToScancode(VK_NUMLOCK);

case SDL_SCANCODE_KP_EQUALS:
return CgvhidClient::VkToScancode(VK_OEM_NEC_EQUAL);
case SDL_SCANCODE_AC_BACK:
return CgvhidClient::VkToScancode(VK_BROWSER_BACK);
case SDL_SCANCODE_AC_FORWARD:
return CgvhidClient::VkToScancode(VK_BROWSER_FORWARD);
case SDL_SCANCODE_AC_REFRESH:
return CgvhidClient::VkToScancode(VK_BROWSER_REFRESH);
case SDL_SCANCODE_AC_STOP:
return CgvhidClient::VkToScancode(VK_BROWSER_STOP);
case SDL_SCANCODE_AC_SEARCH:
return CgvhidClient::VkToScancode(VK_BROWSER_SEARCH);
case SDL_SCANCODE_AC_BOOKMARKS:
return CgvhidClient::VkToScancode(VK_BROWSER_FAVORITES);
case SDL_SCANCODE_AC_HOME:
return CgvhidClient::VkToScancode(VK_BROWSER_HOME);
case SDL_SCANCODE_AUDIOMUTE:
return CgvhidClient::VkToScancode(VK_VOLUME_MUTE);
case SDL_SCANCODE_VOLUMEDOWN:
return CgvhidClient::VkToScancode(VK_VOLUME_DOWN);
case SDL_SCANCODE_VOLUMEUP:
return CgvhidClient::VkToScancode(VK_VOLUME_UP);

case SDL_SCANCODE_AUDIONEXT:
return CgvhidClient::VkToScancode(VK_MEDIA_NEXT_TRACK);
case SDL_SCANCODE_AUDIOPREV:
return CgvhidClient::VkToScancode(VK_MEDIA_PREV_TRACK);
case SDL_SCANCODE_AUDIOSTOP:
return CgvhidClient::VkToScancode(VK_MEDIA_STOP);
case SDL_SCANCODE_AUDIOPLAY:
return CgvhidClient::VkToScancode(VK_MEDIA_PLAY_PAUSE);
case SDL_SCANCODE_MAIL:
return CgvhidClient::VkToScancode(VK_LAUNCH_MAIL);
case SDL_SCANCODE_MEDIASELECT:
return CgvhidClient::VkToScancode(VK_LAUNCH_MEDIA_SELECT);

case SDL_SCANCODE_NONUSBACKSLASH:
return CgvhidClient::VkToScancode(VK_OEM_102);

case SDL_SCANCODE_SYSREQ:
return CgvhidClient::VkToScancode(VK_ATTN);
case SDL_SCANCODE_CRSEL:
return CgvhidClient::VkToScancode(VK_CRSEL);
case SDL_SCANCODE_EXSEL:
return CgvhidClient::VkToScancode(VK_EXSEL);
case SDL_SCANCODE_CLEAR:
return CgvhidClient::VkToScancode(VK_OEM_CLEAR);

case SDL_SCANCODE_APP1:
return CgvhidClient::VkToScancode(VK_LAUNCH_APP1);
case SDL_SCANCODE_APP2:
return CgvhidClient::VkToScancode(VK_LAUNCH_APP2);

default:
if (key_code & 0xFF00) {
return KEY_NONE;
} else {
return static_cast<uint8_t>(key_code);
}
}
}
} // namespace

UdpServer::UdpServer(Engine& engine,
udp::endpoint endpoint,
std::vector<uint8_t> disable_keys,
KeyboardReplay keyboard_replay,
GamepadReplay gamepad_replay)
: engine_(engine),
socket_(engine.GetIoContext(), endpoint),
keyboard_replay_(keyboard_replay),
gamepad_replay_(gamepad_replay) {
for (const auto& e : disable_keys) {
disable_keys_[e] = true;
}

if (KeyboardReplay::CGVHID == keyboard_replay) {
cgvhid_client_.Init(0, 0);
int error_code = cgvhid_client_.KeyboardReset();
Expand Down Expand Up @@ -208,14 +292,20 @@ void UdpServer::OnKeyboardEvent(std::size_t bytes_transferred,
}
if (KeyboardReplay::CGVHID == keyboard_replay_) {
uint16_t key_code = ntohs(control_element->keyboard.key_code);
if (key_code & 0xFF00) {
std::cout << "Unknown key code: " << key_code;
uint8_t scan_code = ScanCodeMap(key_code);
if (KEY_NONE == scan_code) {
std::cout << "Unknown key code: " << key_code << '\n';
return;
}
if (disable_keys_[scan_code]) {
std::cout << "Disabled scan code: " << static_cast<int>(scan_code)
<< '\n';
return;
}
if (ControlButtonState::Pressed == control_element->keyboard.state) {
cgvhid_client_.KeyboardPress(static_cast<uint8_t>(key_code));
cgvhid_client_.KeyboardPress(scan_code);
} else {
cgvhid_client_.KeyboardRelease(static_cast<uint8_t>(key_code));
cgvhid_client_.KeyboardRelease(scan_code);
}
}
}
Expand All @@ -233,6 +323,7 @@ void UdpServer::OnKeyboardVkEvent(std::size_t bytes_transferred,
std::cout << "Unknown key code: " << key_code;
return;
}
// TO-DO: disable-keys
if (ControlButtonState::Pressed == control_element->keyboard.state) {
cgvhid_client_.KeyboardVkPress(static_cast<uint8_t>(key_code));
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/cge/cge/udp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class UdpServer : public std::enable_shared_from_this<UdpServer> {
public:
UdpServer(Engine& engine,
udp::endpoint endpoint,
std::vector<uint8_t> disable_keys,
KeyboardReplay keyboard_replay,
GamepadReplay gamepad_replay);
~UdpServer();
Expand Down Expand Up @@ -80,6 +81,7 @@ class UdpServer : public std::enable_shared_from_this<UdpServer> {
udp::endpoint remote_endpoint_;
std::array<char, 65536> recv_buffer_{};

std::array<bool, 256> disable_keys_{};
KeyboardReplay keyboard_replay_;
CgvhidClient cgvhid_client_;

Expand Down
14 changes: 14 additions & 0 deletions src/cgvhid/cgvhid.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cgvhid_test", "cgvhid_test\
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "cgvhid_setup", "cgvhid_setup\cgvhid_setup.wixproj", "{E1A5B6AC-F386-4D49-86AF-FEA918ED1188}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cgvhid_reset", "cgvhid_reset\cgvhid_reset.vcxproj", "{91B2FBA6-D716-4884-A702-D3A65396144A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Expand Down Expand Up @@ -67,6 +69,18 @@ Global
{E1A5B6AC-F386-4D49-86AF-FEA918ED1188}.Release|x64.ActiveCfg = Release|x64
{E1A5B6AC-F386-4D49-86AF-FEA918ED1188}.Release|x86.ActiveCfg = Release|x86
{E1A5B6AC-F386-4D49-86AF-FEA918ED1188}.Release|x86.Build.0 = Release|x86
{91B2FBA6-D716-4884-A702-D3A65396144A}.Debug|ARM.ActiveCfg = Debug|Win32
{91B2FBA6-D716-4884-A702-D3A65396144A}.Debug|ARM64.ActiveCfg = Debug|Win32
{91B2FBA6-D716-4884-A702-D3A65396144A}.Debug|x64.ActiveCfg = Debug|x64
{91B2FBA6-D716-4884-A702-D3A65396144A}.Debug|x64.Build.0 = Debug|x64
{91B2FBA6-D716-4884-A702-D3A65396144A}.Debug|x86.ActiveCfg = Debug|Win32
{91B2FBA6-D716-4884-A702-D3A65396144A}.Debug|x86.Build.0 = Debug|Win32
{91B2FBA6-D716-4884-A702-D3A65396144A}.Release|ARM.ActiveCfg = Release|Win32
{91B2FBA6-D716-4884-A702-D3A65396144A}.Release|ARM64.ActiveCfg = Release|Win32
{91B2FBA6-D716-4884-A702-D3A65396144A}.Release|x64.ActiveCfg = Release|x64
{91B2FBA6-D716-4884-A702-D3A65396144A}.Release|x64.Build.0 = Release|x64
{91B2FBA6-D716-4884-A702-D3A65396144A}.Release|x86.ActiveCfg = Release|Win32
{91B2FBA6-D716-4884-A702-D3A65396144A}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
26 changes: 26 additions & 0 deletions src/cgvhid/cgvhid_reset/cgvhid_reset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2020-present Ksyun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "cgvhid_client.h"

int main() {
CgvhidClient cgvhid_client;

cgvhid_client.Init(3300, 2200);
cgvhid_client.KeyboardReset();

return 0;
}
Loading

0 comments on commit 732bf07

Please sign in to comment.