Debian Codegraph @

src/support/notification_win32.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2023 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <support/notification.h>
14
15 #include <boca.h>
16
17 #include <windows.h>
18 #include <dbt.h>
19
20 using namespace BoCA;
21 using namespace BoCA::AS;
22
23 static Int GetDriveNumber(const DEV_BROADCAST_VOLUME &dbcv)
24 {
25 /* Get drive number from broadcast structure.
26 */
27 Int number = 0;
28
29 for (Int drive = 0; drive < 26; drive++)
30 {
31 if (dbcv.dbcv_unitmask >> drive & 1) return number;
32
33 String driveLetter = String(" :");
34
35 driveLetter[0] = drive + 'A';
36
37 if (GetDriveType(driveLetter) == DRIVE_CDROM) number++;
38 }
39
40 return -1;
41 }
42
43 static Bool trayOpen[26] = { False };
44 static Bool discLoaded[26] = { False };
45 static UnsignedInt64 discInsertTime[26] = { 0 };
46
47 static Void InitDriveStatus()
48 {
49 Registry &boca = Registry::Get();
50 DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
51
52 if (info != NIL)
53 {
54 for (Int drive = 0; drive < info->GetNumberOfDevices(); drive++)
55 {
56 trayOpen[drive] = info->IsNthDeviceTrayOpen(drive);
57 discLoaded[drive] = info->GetNthDeviceMCDI(drive).IsValid();
58 }
59
60 boca.DeleteComponent(info);
61 }
62 }
63
64 static Void CheckDriveStatus()
65 {
66 Registry &boca = Registry::Get();
67 DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
68
69 if (info != NIL)
70 {
71 for (Int drive = 0; drive < info->GetNumberOfDevices(); drive++)
72 {
73 Bool trayStatus = info->IsNthDeviceTrayOpen(drive);
74
75 if (trayOpen[drive] != trayStatus)
76 {
77 trayOpen[drive] = trayStatus;
78
79 if (trayOpen[drive])
80 {
81 freac::Notification::Get()->onDiscRemove.Emit(drive);
82
83 discLoaded[drive] = False;
84 discInsertTime[drive] = 0;
85 }
86 }
87 else if (!trayOpen[drive] && !discLoaded[drive])
88 {
89 if (info->GetNthDeviceMCDI(drive).IsValid())
90 {
91 freac::Notification::Get()->onDiscInsert.Emit(drive);
92
93 discLoaded[drive] = True;
94 discInsertTime[drive] = S::System::System::Clock();
95 }
96 }
97 }
98
99 boca.DeleteComponent(info);
100 }
101 }
102
103 freac::Notification *freac::Notification::instance = NIL;
104
105 freac::Notification::Notification()
106 {
107 InitDriveStatus();
108
109 Timer *checkDriveStatusTimer = new Timer();
110
111 checkDriveStatusTimer->onInterval.Connect(&CheckDriveStatus);
112 checkDriveStatusTimer->Start(500);
113
114 privateData = checkDriveStatusTimer;
115 }
116
117 freac::Notification::~Notification()
118 {
119 Timer *checkDriveStatusTimer = (Timer *) privateData;
120
121 delete checkDriveStatusTimer;
122 }
123
124 freac::Notification *freac::Notification::Get()
125 {
126 if (instance == NIL)
127 {
128 instance = new Notification();
129 }
130
131 return instance;
132 }
133
134 Void freac::Notification::Free()
135 {
136 if (instance != NIL)
137 {
138 delete instance;
139
140 instance = NIL;
141 }
142 }
143
144 Bool freac::Notification::IsNotificationAvailable() const
145 {
146 return True;
147 }
148
149 Void freac::Notification::ProcessSystemMessage(Int message, Int wParam, Int lParam)
150 {
151 if (message != WM_DEVICECHANGE) return;
152
153 DEV_BROADCAST_HDR *broadcastHeader = (DEV_BROADCAST_HDR *) UINT_PTR(lParam);
154 DEV_BROADCAST_VOLUME *broadcastVolume = (DEV_BROADCAST_VOLUME *) UINT_PTR(lParam);
155
156 switch (wParam)
157 {
158 case DBT_DEVICEARRIVAL:
159 if (broadcastHeader->dbch_devicetype != DBT_DEVTYP_VOLUME || !(broadcastVolume->dbcv_flags & DBTF_MEDIA)) break;
160
161 /* Get drive number from message.
162 */
163 {
164 Int drive = GetDriveNumber(*broadcastVolume);
165
166 if (drive >= 0)
167 {
168 /* Guard against duplicate and late notifications.
169 */
170 if (S::System::System::Clock() - discInsertTime[drive] <= 15000) break;
171
172 /* Fire disc insert event.
173 */
174 onDiscInsert.Emit(drive);
175
176 /* Update drive status.
177 */
178 trayOpen[drive] = False;
179 discLoaded[drive] = True;
180 discInsertTime[drive] = S::System::System::Clock();
181 }
182 }
183
184 break;
185 case DBT_DEVICEREMOVECOMPLETE:
186 if (broadcastHeader->dbch_devicetype != DBT_DEVTYP_VOLUME || !(broadcastVolume->dbcv_flags & DBTF_MEDIA)) break;
187
188 /* Get drive number from message.
189 */
190 {
191 Int drive = GetDriveNumber(*broadcastVolume);
192
193 if (drive >= 0)
194 {
195 /* Fire disc removal event.
196 */
197 onDiscRemove.Emit(drive);
198
199 /* Update drive status.
200 */
201 trayOpen[drive] = True;
202 discLoaded[drive] = False;
203 discInsertTime[drive] = 0;
204 }
205 }
206
207 break;
208 }
209 }