This repository was archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMicMute.cpp
82 lines (77 loc) · 2.16 KB
/
MicMute.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "stdafx.h"
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <stdio.h>
enum class Result {
Trouble = -1,
IsNotMuted,
IsMuted,
};
enum class Action {
GetMuted,
SetMute,
SetUnmute,
ToggleMute,
};
static Result impl(Action action) {
// https://stackoverflow.com/a/3046715/5578773
HRESULT hr;
IMMDeviceEnumerator* deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID*)&deviceEnumerator);
if (hr != S_OK) return Result::Trouble;
IMMDevice* defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eCapture, eCommunications, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
if (defaultDevice == NULL) return Result::Trouble;
IAudioEndpointVolume* endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID*)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
BOOL muted;
hr = endpointVolume->GetMute(&muted);
if (action != Action::GetMuted) {
switch (action) {
case Action::SetMute: muted = true; break;
case Action::SetUnmute: muted = false; break;
case Action::ToggleMute: muted = !muted; break;
}
hr = endpointVolume->SetMute(muted, NULL);
if (hr != S_OK) {
printf("SetMute failed, HRESULT %x\n", hr);
}
}
endpointVolume->Release();
return muted ? Result::IsMuted : Result::IsNotMuted;
}
static void pushResult(lua_State* q, Result r) {
if (r == Result::Trouble) {
lua_pushnil(q);
} else {
lua_pushboolean(q, r == Result::IsMuted);
}
}
static int get(lua_State* q) {
auto r = impl(Action::GetMuted);
pushResult(q, r);
return 1;
}
static int set(lua_State* q) {
auto z = lua_toboolean(q, -1);
auto r = impl(z ? Action::SetMute : Action::SetUnmute);
pushResult(q, r);
return 1;
}
static int toggle(lua_State* q) {
auto z = lua_toboolean(q, -1);
auto r = impl(Action::ToggleMute);
pushResult(q, r);
return 1;
}
void init_micmute_lualib(lua_State* q) {
lua_newtable(q);
luaM_rawset_str_cfunc(q, "get", get);
luaM_rawset_str_cfunc(q, "set", set);
luaM_rawset_str_cfunc(q, "toggle", toggle);
lua_setglobal(q, "micmute");
}