forked from grayver/mi-vigem
-
Notifications
You must be signed in to change notification settings - Fork 13
/
tray.c
280 lines (258 loc) · 6.94 KB
/
tray.c
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* tray.c -- Routines for providing the tray control.
*/
#include <tchar.h>
#include <windows.h>
#include <shellapi.h>
#include <dbt.h>
#include "tray.h"
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "shell32.lib")
#define WM_TRAY_CALLBACK_MESSAGE (WM_USER + 1)
#define WC_TRAY_CLASS_NAME TEXT("StadiaViGEmClass")
#define WC_TRAY_MUTEX_NAME TEXT("Stadia Controller")
#define ID_TRAY_FIRST 1000
static WNDCLASSEX wc;
static NOTIFYICONDATA nid;
static HWND window_handle = NULL;
static HMENU hmenu = NULL;
static HANDLE hmutex;
static HDEVNOTIFY hdevntf;
static void (*devntf_cb)(UINT op, LPTSTR path) = NULL;
static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CLOSE:
if (hdevntf != NULL)
{
UnregisterDeviceNotification(hdevntf);
}
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_TRAY_CALLBACK_MESSAGE:
if (lparam == WM_LBUTTONUP || lparam == WM_RBUTTONUP)
{
POINT p;
GetCursorPos(&p);
SetForegroundWindow(hwnd);
BOOL cmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
p.x, p.y, 0, hwnd, NULL);
SendMessage(hwnd, WM_COMMAND, cmd, 0);
return 0;
}
break;
case WM_COMMAND:
if (wparam >= ID_TRAY_FIRST)
{
MENUITEMINFO item = {
.cbSize = sizeof(MENUITEMINFO),
.fMask = MIIM_ID | MIIM_DATA,
};
if (GetMenuItemInfo(hmenu, wparam, FALSE, &item))
{
struct tray_menu *menu = (struct tray_menu *)item.dwItemData;
if (menu != NULL && menu->cb != NULL)
{
menu->cb(menu);
}
}
return 0;
}
break;
case WM_DEVICECHANGE:
if (devntf_cb != NULL)
{
UINT op = DO_TRAY_UNKNOWN;
switch (wparam)
{
case DBT_DEVICEARRIVAL:
op = DO_TRAY_DEV_ATTACHED;
break;
case DBT_DEVICEREMOVECOMPLETE:
op = DO_TRAY_DEV_REMOVED;
break;
}
LPTSTR path = NULL;
if (lparam != 0)
{
PDEV_BROADCAST_DEVICEINTERFACE bdi = (PDEV_BROADCAST_DEVICEINTERFACE)lparam;
path = bdi->dbcc_name;
}
devntf_cb(op, path);
}
break;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
static HMENU _tray_menu(struct tray_menu *m, UINT *id)
{
HMENU new_menu = CreatePopupMenu();
for (; m != NULL && m->text != NULL; m++, (*id)++)
{
if (_tcscmp(m->text, TEXT("-")) == 0)
{
InsertMenu(new_menu, *id, MF_SEPARATOR, TRUE, TEXT(""));
}
else
{
MENUITEMINFO item;
memset(&item, 0, sizeof(item));
item.cbSize = sizeof(MENUITEMINFO);
item.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_DATA;
item.fType = 0;
item.fState = 0;
if (m->submenu != NULL)
{
item.fMask = item.fMask | MIIM_SUBMENU;
item.hSubMenu = _tray_menu(m->submenu, id);
}
if (m->disabled)
{
item.fState |= MFS_DISABLED;
}
if (m->checked)
{
item.fState |= MFS_CHECKED;
}
item.wID = *id;
item.dwTypeData = m->text;
item.dwItemData = (ULONG_PTR)m;
InsertMenuItem(new_menu, *id, TRUE, &item);
}
}
return new_menu;
}
int tray_init(struct tray *tray)
{
hmutex = CreateMutex(NULL, TRUE, WC_TRAY_MUTEX_NAME);
if (hmutex == NULL)
{
return -1;
}
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = _tray_wnd_proc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = WC_TRAY_CLASS_NAME;
if (!RegisterClassEx(&wc))
{
return -1;
}
window_handle = CreateWindowEx(0, WC_TRAY_CLASS_NAME, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if (window_handle == NULL)
{
return -1;
}
UpdateWindow(window_handle);
memset(&nid, 0, sizeof(nid));
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = window_handle;
nid.uID = 0;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_TRAY_CALLBACK_MESSAGE;
Shell_NotifyIcon(NIM_ADD, &nid);
tray_update(tray);
return 0;
}
int tray_loop(BOOLEAN blocking)
{
MSG msg;
if (blocking)
{
GetMessage(&msg, NULL, 0, 0);
}
else
{
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
}
if (msg.message == WM_QUIT)
{
return -1;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
return 0;
}
void tray_update(struct tray *tray)
{
HMENU prevmenu = hmenu;
UINT id = ID_TRAY_FIRST;
hmenu = _tray_menu(tray->menu, &id);
SendMessage(window_handle, WM_INITMENUPOPUP, (WPARAM)hmenu, 0);
HICON hicon = LoadIcon(wc.hInstance, tray->icon);
if (nid.hIcon)
{
DestroyIcon(nid.hIcon);
}
nid.hIcon = hicon;
_tcscpy(nid.szTip, tray->tip);
Shell_NotifyIcon(NIM_MODIFY, &nid);
if (prevmenu != NULL)
{
DestroyMenu(prevmenu);
}
}
void tray_exit()
{
Shell_NotifyIcon(NIM_DELETE, &nid);
if (nid.hIcon != 0)
{
DestroyIcon(nid.hIcon);
}
if (hmenu != 0)
{
DestroyMenu(hmenu);
}
PostQuitMessage(0);
window_handle = NULL;
UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
ReleaseMutex(hmutex);
CloseHandle(hmutex);
}
void tray_register_device_notification(GUID filter, void (*cb)(UINT, LPTSTR))
{
if (window_handle == NULL)
{
return;
}
DEV_BROADCAST_DEVICEINTERFACE dbdi;
memset(&dbdi, 0, sizeof(dbdi));
dbdi.dbcc_size = sizeof(dbdi);
dbdi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
dbdi.dbcc_classguid = filter;
hdevntf = RegisterDeviceNotification(window_handle, &dbdi, DEVICE_NOTIFY_WINDOW_HANDLE);
if (hdevntf != NULL)
{
devntf_cb = cb;
}
}
void tray_show_notification(UINT type, LPTSTR title, LPTSTR text)
{
if (window_handle == NULL)
{
return;
}
NOTIFYICONDATA nid_info;
memmove(&nid_info, &nid, sizeof(NOTIFYICONDATA));
nid_info.uFlags |= NIF_INFO;
_tcscpy(nid_info.szInfoTitle, title);
_tcscpy(nid_info.szInfo, text);
switch (type)
{
case NT_TRAY_INFO:
nid_info.dwInfoFlags = NIIF_INFO;
break;
case NT_TRAY_WARNING:
nid_info.dwInfoFlags = NIIF_WARNING;
break;
case NT_TRAY_ERROR:
nid_info.dwInfoFlags = NIIF_ERROR;
break;
}
nid_info.uTimeout = 10000;
Shell_NotifyIcon(NIM_MODIFY, &nid_info);
}