-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicStatusPlugin.cpp
More file actions
258 lines (209 loc) · 8.29 KB
/
Copy pathMicStatusPlugin.cpp
File metadata and controls
258 lines (209 loc) · 8.29 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "PluginInterface.h"
#include <Windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <audiopolicy.h>
#include <string>
#include <mutex>
#include <algorithm>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "gdi32.lib")
// ==================== Mini GDI+ replacement using raw GDI ====================
// We avoid linking gdiplus.dll to keep the plugin self-contained.
static void InitCOM() {
static bool done = false;
if (!done) { CoInitializeEx(nullptr, COINIT_MULTITHREADED); done = true; }
}
// ==================== Audio Helpers ====================
static bool HasActiveCaptureSession() {
InitCOM();
IMMDeviceEnumerator* pEnum = nullptr;
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator), (void**)&pEnum)) || !pEnum) return false;
IMMDevice* pDev = nullptr;
HRESULT hr = pEnum->GetDefaultAudioEndpoint(eCapture, eConsole, &pDev);
pEnum->Release();
if (FAILED(hr) || !pDev) return false;
IAudioSessionManager2* pMgr = nullptr;
hr = pDev->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL,
nullptr, (void**)&pMgr);
pDev->Release();
if (FAILED(hr) || !pMgr) return false;
IAudioSessionEnumerator* pList = nullptr;
hr = pMgr->GetSessionEnumerator(&pList);
pMgr->Release();
if (FAILED(hr) || !pList) return false;
int count = 0;
pList->GetCount(&count);
bool active = false;
for (int i = 0; i < count; i++) {
IAudioSessionControl* pCtrl = nullptr;
if (FAILED(pList->GetSession(i, &pCtrl)) || !pCtrl) continue;
AudioSessionState state;
if (SUCCEEDED(pCtrl->GetState(&state)) && state == AudioSessionStateActive) {
pCtrl->Release();
active = true;
break;
}
pCtrl->Release();
}
pList->Release();
return active;
}
static bool IsMicMuted() {
InitCOM();
IMMDeviceEnumerator* pEnum = nullptr;
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator), (void**)&pEnum)) || !pEnum) return false;
IMMDevice* pDev = nullptr;
HRESULT hr = pEnum->GetDefaultAudioEndpoint(eCapture, eConsole, &pDev);
pEnum->Release();
if (FAILED(hr) || !pDev) return false;
IAudioEndpointVolume* pVol = nullptr;
hr = pDev->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL,
nullptr, (void**)&pVol);
pDev->Release();
if (FAILED(hr) || !pVol) return false;
BOOL muted = FALSE;
pVol->GetMute(&muted);
pVol->Release();
return muted != FALSE;
}
static void ToggleMicMute() {
InitCOM();
IMMDeviceEnumerator* pEnum = nullptr;
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator), (void**)&pEnum)) || !pEnum) return;
IMMDevice* pDev = nullptr;
HRESULT hr = pEnum->GetDefaultAudioEndpoint(eCapture, eConsole, &pDev);
pEnum->Release();
if (FAILED(hr) || !pDev) return;
IAudioEndpointVolume* pVol = nullptr;
hr = pDev->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL,
nullptr, (void**)&pVol);
pDev->Release();
if (FAILED(hr) || !pVol) return;
BOOL muted = FALSE;
pVol->GetMute(&muted);
pVol->SetMute(!muted, nullptr);
pVol->Release();
}
// ==================== Plugin Item ====================
class MicStatusItem : public IPluginItem {
mutable bool m_micActive;
mutable bool m_muted;
mutable int m_width;
public:
MicStatusItem() : m_micActive(false), m_muted(false), m_width(0) {}
void UpdateStatus(bool active, bool muted) {
m_micActive = active;
m_muted = muted;
m_width = active ? 28 : 0;
}
// --- IPluginItem (all const where interface requires) ---
const wchar_t* GetItemName() const override { return L"Mic Status"; }
const wchar_t* GetItemId() const override { return L"MicStatus"; }
const wchar_t* GetItemLableText() const override { return m_micActive ? (m_muted ? L"MUT" : L"MIC") : L""; }
const wchar_t* GetItemValueText() const override { return m_micActive ? (m_muted ? L"Muted" : L"Active") : L""; }
const wchar_t* GetItemValueSampleText() const override { return L"Muted"; }
bool IsCustomDraw() const override { return true; }
int GetItemWidth() const override { return m_width; }
int GetItemWidthEx(void* hDC) const override { return m_width; }
void DrawItem(void* hDC, int x, int y, int w, int h, bool dark_mode) override {
if (!m_micActive) return;
HDC hdc = (HDC)hDC;
int saved = SaveDC(hdc);
int sz = (std::min)(w, h) - 4;
if (sz < 10) sz = 16;
int cx = x + (w - sz) / 2;
int cy = y + (h - sz) / 2;
COLORREF bg = m_muted ? RGB(220, 60, 50) : RGB(76, 175, 80);
COLORREF fg = dark_mode ? RGB(240, 240, 240) : RGB(255, 255, 255);
HBRUSH bgBr = CreateSolidBrush(bg);
HPEN bgPn = CreatePen(PS_NULL, 0, 0);
SelectObject(hdc, bgBr); SelectObject(hdc, bgPn);
Ellipse(hdc, cx + 1, cy + 1, cx + sz - 1, cy + sz - 1);
float s = sz / 24.0f;
HBRUSH br = CreateSolidBrush(fg);
HPEN pn = CreatePen(PS_SOLID, (std::max)(1, sz / 12), fg);
SelectObject(hdc, br); SelectObject(hdc, pn);
int bl = cx + (int)(7*s), bt = cy + (int)(3*s);
int brr= cx + (int)(13*s), bb= cy + (int)(14*s);
Rectangle(hdc, bl, bt, brr + 1, bb + 1);
Rectangle(hdc, bl, bb, brr + 1, cy + (int)(17*s) + 1);
int mx = cx + (int)(10*s);
MoveToEx(hdc, mx, cy + (int)(17*s), nullptr);
LineTo(hdc, mx, cy + (int)(20*s));
MoveToEx(hdc, cx + (int)(7*s), cy + (int)(20*s), nullptr);
LineTo(hdc, cx + (int)(13*s), cy + (int)(20*s));
if (m_muted) {
HPEN sp = CreatePen(PS_SOLID, (std::max)(1, sz / 10), fg);
SelectObject(hdc, sp);
MoveToEx(hdc, cx + (int)(3*s), cy + (int)(3*s), nullptr);
LineTo(hdc, cx + (int)(17*s), cy + (int)(17*s));
DeleteObject(sp);
}
DeleteObject(br); DeleteObject(pn);
DeleteObject(bgBr); DeleteObject(bgPn);
RestoreDC(hdc, saved);
}
int OnMouseEvent(MouseEventType type, int x, int y, void* hWnd, int flag) override {
if (!m_micActive) return 0;
if (type == MT_LCLICKED) {
ToggleMicMute();
return 1;
}
return 0;
}
};
// ==================== Plugin ====================
class MicStatusPlugin : public ITMPlugin {
MicStatusItem m_item;
ITrafficMonitor* m_host;
std::mutex m_mutex;
int m_tick;
static const wchar_t* kInfo[];
public:
MicStatusPlugin() : m_host(nullptr), m_tick(0) {}
IPluginItem* GetItem(int index) override {
return (index == 0) ? &m_item : nullptr;
}
const wchar_t* GetInfo(PluginInfoIndex idx) override {
int i = (int)idx;
return (i >= 0 && i < (int)TMI_MAX) ? kInfo[i] : L"";
}
void OnInitialize(ITrafficMonitor* host) override { m_host = host; }
void DataRequired() override {
// Throttle to once per 200ms
static ULONGLONG lastCheck = 0;
ULONGLONG now = GetTickCount64();
if (now - lastCheck < 200) return;
lastCheck = now;
std::lock_guard<std::mutex> lk(m_mutex);
m_item.UpdateStatus(HasActiveCaptureSession(), IsMicMuted());
}
const wchar_t* GetTooltipInfo() override {
if (!HasActiveCaptureSession()) return L"";
return IsMicMuted() ? L"Microphone Muted - Click to unmute"
: L"Microphone Active - Click to mute";
}
int GetCommandCount() override { return 1; }
const wchar_t* GetCommandName(int) override { return L"Toggle Mute"; }
void OnPluginCommand(int, void*, void*) override { ToggleMicMute(); }
};
const wchar_t* MicStatusPlugin::kInfo[] = {
L"Mic Status",
L"Show mic mute status when apps are using the microphone",
L"Claude",
L"",
L"1.0.0",
L"",
};
// ==================== Export ====================
extern "C" __declspec(dllexport) ITMPlugin* TMPluginGetInstance() {
static MicStatusPlugin inst;
return &inst;
}
BOOL APIENTRY DllMain(HMODULE, DWORD reason, LPVOID) {
return TRUE;
}