Skip to content

Commit

Permalink
Added source code
Browse files Browse the repository at this point in the history
  • Loading branch information
DocSystem committed Oct 26, 2020
1 parent 9c5b0b5 commit 3d118a4
Show file tree
Hide file tree
Showing 4 changed files with 409 additions and 0 deletions.
209 changes: 209 additions & 0 deletions micmutetokey.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#include <windows.h>
#include <stdio.h>

#include <mmdeviceapi.h>
#include <devicetopology.h>
#include <WinUser.h>

HRESULT WalkTreeBackwardsFromPart(IPart* pPart);
HRESULT DisplayMute(IAudioMute* pMute);

int MuteStatus{ 0 };

int getMicrophoneMuteStatus() {
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) {
printf("Failed CoInitializeEx: hr = 0x%08x\n", hr);
return __LINE__;
}

// get default render endpoint
IMMDeviceEnumerator* pEnum = NULL;
hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
(void**)&pEnum
);
if (FAILED(hr)) {
printf("Couldn't get device enumerator: hr = 0x%08x\n", hr);
CoUninitialize();
return __LINE__;
}
IMMDevice* pDevice = NULL;
hr = pEnum->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
if (FAILED(hr)) {
printf("Couldn't get default render device: hr = 0x%08x\n", hr);
pEnum->Release();
CoUninitialize();
return __LINE__;
}
pEnum->Release();

// get device topology object for that endpoint
IDeviceTopology* pDT = NULL;
hr = pDevice->Activate(__uuidof(IDeviceTopology), CLSCTX_ALL, NULL, (void**)&pDT);
if (FAILED(hr)) {
printf("Couldn't get device topology object: hr = 0x%08x\n", hr);
pDevice->Release();
CoUninitialize();
return __LINE__;
}
pDevice->Release();

// get the single connector for that endpoint
IConnector* pConnEndpoint = NULL;
hr = pDT->GetConnector(0, &pConnEndpoint);
if (FAILED(hr)) {
printf("Couldn't get the connector on the endpoint: hr = 0x%08x\n", hr);
pDT->Release();
CoUninitialize();
return __LINE__;
}
pDT->Release();

// get the connector on the device that is
// connected to
// the connector on the endpoint
IConnector* pConnDevice = NULL;
hr = pConnEndpoint->GetConnectedTo(&pConnDevice);
if (FAILED(hr)) {
printf("Couldn't get the connector on the device: hr = 0x%08x\n", hr);
pConnEndpoint->Release();
CoUninitialize();
return __LINE__;
}
pConnEndpoint->Release();

// QI on the device's connector for IPart
IPart* pPart = NULL;
hr = pConnDevice->QueryInterface(__uuidof(IPart), (void**)&pPart);
if (FAILED(hr)) {
printf("Couldn't get the part: hr = 0x%08x\n", hr);
pConnDevice->Release();
CoUninitialize();
return __LINE__;
}
pConnDevice->Release();

// all the real work is done in this function
MuteStatus = 0;
hr = WalkTreeBackwardsFromPart(pPart);
if (FAILED(hr)) {
printf("Couldn't walk the tree: hr = 0x%08x\n", hr);
pPart->Release();
CoUninitialize();
return __LINE__;
}
pPart->Release();

CoUninitialize();

return hr;
}

HRESULT WalkTreeBackwardsFromPart(IPart* pPart) {
HRESULT hr = S_OK;

LPWSTR pwszPartName = NULL;
hr = pPart->GetName(&pwszPartName);
//printf("Part name: %ws\n", *pwszPartName ? pwszPartName : L"(Unnamed)");
CoTaskMemFree(pwszPartName);

// see if this is a mute node part
IAudioMute* pMute = NULL;
hr = pPart->Activate(CLSCTX_ALL, __uuidof(IAudioMute), (void**)&pMute);
if (E_NOINTERFACE == hr) {
// not a mute node
}
else if (FAILED(hr)) {
printf("Unexpected failure trying to activate IAudioMute: hr = 0x%08x\n", hr);
return hr;
}
else {
// it's a mute node...
hr = DisplayMute(pMute);
if (FAILED(hr)) {
printf("DisplayMute failed: hr = 0x%08x", hr);
pMute->Release();
return hr;
}
if (hr == 1) {
MuteStatus = 1;
}
pMute->Release();
}

// get the list of incoming parts
IPartsList* pIncomingParts = NULL;
hr = pPart->EnumPartsIncoming(&pIncomingParts);
if (E_NOTFOUND == hr) {
// not an error... we've just reached the end of the path
// printf("No incoming parts at this part\n");
return S_OK;
}
if (FAILED(hr)) {
printf("Couldn't enum incoming parts: hr = 0x%08x\n", hr);
return hr;
}
UINT nParts = 0;
hr = pIncomingParts->GetCount(&nParts);
if (FAILED(hr)) {
printf("Couldn't get count of incoming parts: hr = 0x%08x\n", hr);
pIncomingParts->Release();
return hr;
}

// walk the tree on each incoming part recursively
for (UINT n = 0; n < nParts; n++) {
IPart* pIncomingPart = NULL;
hr = pIncomingParts->GetPart(n, &pIncomingPart);

hr = WalkTreeBackwardsFromPart(pIncomingPart);
pIncomingPart->Release();
}

pIncomingParts->Release();

return MuteStatus;
}


HRESULT DisplayMute(IAudioMute* pMute) {
HRESULT hr = S_OK;
BOOL bMuted = FALSE;
hr = pMute->GetMute(&bMuted);

if (FAILED(hr)) {
printf("GetMute failed: hr = 0x%08x\n", hr);
return hr;
}

return bMuted ? 1 : 0;
}


int main()
{
ShowWindow(GetConsoleWindow(), SW_HIDE);
int currentMicMuteStatus = getMicrophoneMuteStatus();
int oldCurrentMicMuteStatus = currentMicMuteStatus;
while (true)
{
Sleep(10);
oldCurrentMicMuteStatus = currentMicMuteStatus;
currentMicMuteStatus = getMicrophoneMuteStatus();
if (oldCurrentMicMuteStatus != currentMicMuteStatus)
{
INPUT inp;
inp.type = INPUT_KEYBOARD;
inp.ki.wScan = 0;
inp.ki.time = 0;
inp.ki.dwExtraInfo = 0;
inp.ki.wVk = 0x82; // virtual-key code for the "f19" key
inp.ki.dwFlags = 0; // 0 for key press
SendInput(1, &inp, sizeof(INPUT));
inp.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &inp, sizeof(INPUT));
}
}
return 0;
}
31 changes: 31 additions & 0 deletions micmutetokey.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30503.244
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "micmutetokey", "micmutetokey.vcxproj", "{681BA3D1-E526-46AF-82BF-3708B7508333}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{681BA3D1-E526-46AF-82BF-3708B7508333}.Debug|x64.ActiveCfg = Debug|x64
{681BA3D1-E526-46AF-82BF-3708B7508333}.Debug|x64.Build.0 = Debug|x64
{681BA3D1-E526-46AF-82BF-3708B7508333}.Debug|x86.ActiveCfg = Debug|Win32
{681BA3D1-E526-46AF-82BF-3708B7508333}.Debug|x86.Build.0 = Debug|Win32
{681BA3D1-E526-46AF-82BF-3708B7508333}.Release|x64.ActiveCfg = Release|x64
{681BA3D1-E526-46AF-82BF-3708B7508333}.Release|x64.Build.0 = Release|x64
{681BA3D1-E526-46AF-82BF-3708B7508333}.Release|x86.ActiveCfg = Release|Win32
{681BA3D1-E526-46AF-82BF-3708B7508333}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {959D599B-F6AE-433E-82F8-ECDC878E5B55}
EndGlobalSection
EndGlobal
147 changes: 147 additions & 0 deletions micmutetokey.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{681ba3d1-e526-46af-82bf-3708b7508333}</ProjectGuid>
<RootNamespace>micmutetokey</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="micmutetokey.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading

0 comments on commit 3d118a4

Please sign in to comment.