From 3d118a4e32ccfe7ba321e2ed688a9b594478e1ed Mon Sep 17 00:00:00 2001 From: DocSystem <42613513+DocSystem@users.noreply.github.com> Date: Mon, 26 Oct 2020 15:03:46 +0100 Subject: [PATCH] Added source code --- micmutetokey.cpp | 209 +++++++++++++++++++++++++++++++++++ micmutetokey.sln | 31 ++++++ micmutetokey.vcxproj | 147 ++++++++++++++++++++++++ micmutetokey.vcxproj.filters | 22 ++++ 4 files changed, 409 insertions(+) create mode 100644 micmutetokey.cpp create mode 100644 micmutetokey.sln create mode 100644 micmutetokey.vcxproj create mode 100644 micmutetokey.vcxproj.filters diff --git a/micmutetokey.cpp b/micmutetokey.cpp new file mode 100644 index 0000000..3f9d5e7 --- /dev/null +++ b/micmutetokey.cpp @@ -0,0 +1,209 @@ +#include +#include + +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/micmutetokey.sln b/micmutetokey.sln new file mode 100644 index 0000000..0069e57 --- /dev/null +++ b/micmutetokey.sln @@ -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 diff --git a/micmutetokey.vcxproj b/micmutetokey.vcxproj new file mode 100644 index 0000000..5274199 --- /dev/null +++ b/micmutetokey.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {681ba3d1-e526-46af-82bf-3708b7508333} + micmutetokey + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/micmutetokey.vcxproj.filters b/micmutetokey.vcxproj.filters new file mode 100644 index 0000000..4745564 --- /dev/null +++ b/micmutetokey.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Fichiers sources + + + \ No newline at end of file