Skip to content

Commit

Permalink
make it build in vs2019 & make it a cli
Browse files Browse the repository at this point in the history
  • Loading branch information
shunf4 committed Aug 1, 2023
1 parent 8a1a5a5 commit ad239a4
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ToothTray/BluetoothAudioDevices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ std::vector<BluetoothConnector> BluetoothAudioDeviceEnumerator::EnumerateAudioDe
std::unordered_map<GUID, std::wstring, GUIDHasher, GUIDEqualityComparer>::const_iterator containerIte = containers.find(containerId);
if (containerIte != containers.cend()) {
const std::wstring& containerName = containerIte->second;
ite = bluetoothConnectors.emplace(std::piecewise_construct, std::forward_as_tuple(containerId), std::forward_as_tuple(containerName)).first;
ite = bluetoothConnectors.emplace(std::piecewise_construct, std::forward_as_tuple(containerId), std::forward_as_tuple(containerName, containerId)).first;
}
}

Expand Down
11 changes: 9 additions & 2 deletions ToothTray/BluetoothAudioDevices.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ class BluetoothConnector {
BluetoothConnector(BluetoothConnector&& other) = default;
BluetoothConnector& operator=(BluetoothConnector&&) = default;

BluetoothConnector(const std::wstring& containerName)
: m_deviceName(containerName), m_isConnected(false) {}
BluetoothConnector(const std::wstring& containerName, const GUID& containerId)
: m_deviceName(containerName), m_containerId(containerId), m_isConnected(false) {}

std::wstring_view DeviceName() const {
return std::wstring_view(m_deviceName);
}

std::wstring_view ContainerId() const {
wchar_t* containerIdBuffer = new wchar_t[200];
StringFromGUID2(m_containerId, containerIdBuffer, 200);
return std::wstring_view(containerIdBuffer);
}

void addConnectorControl(const wil::com_ptr<IKsControl>& connectorControl, DWORD state);

bool IsConnected() {
Expand All @@ -37,6 +43,7 @@ class BluetoothConnector {
}
private:
std::wstring m_deviceName;
GUID m_containerId;
bool m_isConnected;
std::vector<wil::com_ptr<IKsControl>> m_ksControls;

Expand Down
2 changes: 1 addition & 1 deletion ToothTray/BluetoothDeviceWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void OutputDeviceProperties(std::wostringstream& sout, const winrt::Windows::Fou
for (const auto& kvp : properties) {
const winrt::hstring&& propName = kvp.Key();
if (propName == L"System.Devices.Aep.ContainerId") {
winrt::guid containerId = kvp.Value().as<winrt::guid>();
winrt::guid containerId = winrt::unbox_value<winrt::guid>(kvp.Value());
sout << propName.c_str();
}
else {
Expand Down
10 changes: 5 additions & 5 deletions ToothTray/BluetoothRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class BluetoothDevice {
public:
constexpr BluetoothDevice() : m_hRadio(NULL), m_info({ 0 }) {}
BluetoothDevice() : m_hRadio(NULL), m_info({ 0 }) {}
BluetoothDevice(HANDLE hRadio, const BLUETOOTH_DEVICE_INFO& info);

void Enable();
Expand All @@ -21,15 +21,15 @@ class BluetoothDevice {
class BluetoothRadio {
public:
static BluetoothRadio FindFirst();
constexpr BluetoothRadio(std::nullptr_t) : m_hRadio(NULL), m_hNotify(NULL) {}
BluetoothRadio(std::nullptr_t) : m_hRadio(NULL), m_hNotify(NULL) {}
~BluetoothRadio() noexcept;

constexpr BluetoothRadio(BluetoothRadio&& other) noexcept : m_hRadio(other.m_hRadio), m_hNotify(other.m_hNotify)
BluetoothRadio(BluetoothRadio&& other) noexcept : m_hRadio(other.m_hRadio), m_hNotify(other.m_hNotify)
{
other.m_hRadio = other.m_hNotify = NULL;
}

constexpr BluetoothRadio& operator=(BluetoothRadio&& other) noexcept
BluetoothRadio& operator=(BluetoothRadio&& other) noexcept
{
std::swap(m_hRadio, other.m_hRadio);
std::swap(m_hNotify, other.m_hNotify);
Expand All @@ -44,7 +44,7 @@ class BluetoothRadio {
void EnableAudioSink();
void DisableAudioSink();
private:
constexpr BluetoothRadio(HANDLE hRadio) : m_hRadio(hRadio), m_hNotify(NULL) {}
BluetoothRadio(HANDLE hRadio) : m_hRadio(hRadio), m_hNotify(NULL) {}

HANDLE m_hRadio;
HDEVNOTIFY m_hNotify;
Expand Down
201 changes: 201 additions & 0 deletions ToothTray/ToothTray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "framework.h"
#include "ToothTray.h"
#include <memory>
#include <cstdio>
#include <functional>
#include <winrt/base.h>

#include "debuglog.h"
Expand All @@ -29,6 +31,205 @@ BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int wmain(int argc, wchar_t* argv[]) {
winrt::init_apartment();
std::vector<BluetoothConnector> connectors = bluetoothAudioDeviceEmumerator.EnumerateAudioDevices();

std::setlocale(LC_ALL, "");

const wchar_t* command = L"list";
if (argc >= 2) {
command = argv[1];
}

if (lstrcmpW(command, L"list") == 0) {
for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
std::wstring deviceName = std::wstring(ite->DeviceName());
bool checked = ite->IsConnected();

wprintf(L"%ls %ls %ls\n", checked ? L"1" : L"0", std::wstring(ite->ContainerId()).c_str(), deviceName.c_str());
}
return 0;
} else if(lstrcmpW(command, L"is-connected") == 0 && argc >= 3) {
const wchar_t* isConnectedArg = argv[2];
bool found = false;

for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
std::wstring deviceName = std::wstring(ite->DeviceName());
bool checked = ite->IsConnected();

if (lstrcmpW(isConnectedArg, deviceName.c_str()) == 0) {
wprintf(checked ? L"1\n" : L"0\n");
found = true;
break;
}
}
if (!found) {
fwprintf(stderr, L"device not found (2)\n");
return 2;
}
return 0;
}
else if (lstrcmpW(command, L"is-connected-by-container-id") == 0 && argc >= 3) {
const wchar_t* isConnectedArg = argv[2];
bool found = false;

for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
bool checked = ite->IsConnected();

if (lstrcmpW(isConnectedArg, std::wstring(ite->ContainerId()).c_str()) == 0) {
wprintf(checked ? L"1\n" : L"0\n");
found = true;
break;
}
}
if (!found) {
fwprintf(stderr, L"device not found (2)\n");
return 2;
}
return 0;
}
else if (lstrcmpW(command, L"connect") == 0 && argc >= 3) {
const wchar_t* connectArg = argv[2];

bool force = false;
if (argc >= 4 && lstrcmpW(argv[2], L"-f") == 0) {
connectArg = argv[3];
force = true;
}

bool found = false;

for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
if (lstrcmpW(connectArg, std::wstring(ite->DeviceName()).c_str()) == 0) {
ite->Connect();
found = true;
break;
}
}
if (!found) {
fwprintf(stderr, L"device not found (2)\n");
return 2;
}
return 0;
}
else if (lstrcmpW(command, L"connect-by-container-id") == 0 && argc >= 3) {
const wchar_t* connectArg = argv[2];

bool force = false;
if (argc >= 4 && lstrcmpW(argv[2], L"-f") == 0) {
connectArg = argv[3];
force = true;
}

bool found = false;

for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
if (lstrcmpW(connectArg, std::wstring(ite->ContainerId()).c_str()) == 0) {
if (force || !ite->IsConnected()) {
ite->Connect();
}
found = true;
break;
}
}
if (!found) {
fwprintf(stderr, L"device not found (2)\n");
return 2;
}
return 0;
}
else if (lstrcmpW(command, L"connect-by-container-id") == 0 && argc >= 3) {
const wchar_t* connectArg = argv[2];

bool force = false;
if (argc >= 4 && lstrcmpW(argv[2], L"-f") == 0) {
connectArg = argv[3];
force = true;
}

bool found = false;

for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
if (lstrcmpW(connectArg, std::wstring(ite->ContainerId()).c_str()) == 0) {
if (force || !ite->IsConnected()) {
ite->Connect();
}
found = true;
break;
}
}
if (!found) {
fwprintf(stderr, L"device not found (2)\n");
return 2;
}
return 0;
}
else if (lstrcmpW(command, L"disconnect") == 0 && argc >= 3) {
const wchar_t* connectArg = argv[2];

bool force = false;
if (argc >= 4 && lstrcmpW(argv[2], L"-f") == 0) {
connectArg = argv[3];
force = true;
}

bool found = false;

for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
if (lstrcmpW(connectArg, std::wstring(ite->DeviceName()).c_str()) == 0) {
if (force || ite->IsConnected()) {
ite->Disconnect();
}
found = true;
break;
}
}
if (!found) {
fwprintf(stderr, L"device not found (2)\n");
return 2;
}
return 0;
}
else if (lstrcmpW(command, L"disconnect-by-container-id") == 0 && argc >= 3) {
const wchar_t* connectArg = argv[2];

bool force = false;
if (argc >= 4 && lstrcmpW(argv[2], L"-f") == 0) {
connectArg = argv[3];
force = true;
}

bool found = false;

for (std::vector<BluetoothConnector>::iterator ite = connectors.begin(); ite != connectors.end(); ++ite) {
if (lstrcmpW(connectArg, std::wstring(ite->ContainerId()).c_str()) == 0) {
if (force || ite->IsConnected()) {
ite->Disconnect();
}
found = true;
break;
}
}
if (!found) {
fwprintf(stderr, L"device not found (2)\n");
return 2;
}
return 0;
}

const wchar_t* helpText = L"toothtray-cli list\ntoothtray-cli is-connected \"My Device\"\ntoothtray-cli is-connected-by-container-id {xxx}\ntoothtray-cli connect[-by-container-id] [-f] ...\ntoothtray-cli disconnect[-by-container-id] [-f] ...\n";

if (lstrcmpW(command, L"-h") == 0 || lstrcmpW(command, L"--help") == 0) {
fwprintf(stdout, helpText);
}
else {
fwprintf(stderr, helpText);
}

return 0;
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
Expand Down
13 changes: 8 additions & 5 deletions ToothTray/ToothTray.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down Expand Up @@ -56,11 +56,12 @@
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Ws2_32.lib;Bthprops.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Ws2_32.lib;Bthprops.lib;windowsapp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -71,13 +72,15 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Ws2_32.lib;Bthprops.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Ws2_32.lib;Bthprops.lib;windowsapp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down

0 comments on commit ad239a4

Please sign in to comment.