Debian Codegraph @

src/support/notification_udev.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2018 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 <libudev.h>
18
19 using namespace BoCA;
20 using namespace BoCA::AS;
21
22 using namespace smooth::System;
23
24 static DynamicLoader *loader = NIL;
25
26 /* udev status variables.
27 */
28 static struct udev *udev = NIL;
29 static struct udev_monitor *udevMonitor = NIL;
30 static int udevFd = 0;
31
32 /* udev function pointers.
33 */
34 static struct udev * (*ex_udev_new)(void) = NIL;
35 static void (*ex_udev_unref)(struct udev *) = NIL;
36
37 static const char * (*ex_udev_device_get_devnode)(struct udev_device *) = NIL;
38 static const char * (*ex_udev_device_get_property_value)(struct udev_device *, const char *) = NIL;
39 static void (*ex_udev_device_unref)(struct udev_device *) = NIL;
40
41 static struct udev_monitor * (*ex_udev_monitor_new_from_netlink)(struct udev *, const char *) = NIL;
42 static int (*ex_udev_monitor_filter_add_match_subsystem_devtype)(struct udev_monitor *, const char *, const char *) = NIL;
43 static int (*ex_udev_monitor_enable_receiving)(struct udev_monitor *) = NIL;
44 static int (*ex_udev_monitor_get_fd)(struct udev_monitor *) = NIL;
45 static struct udev_device * (*ex_udev_monitor_receive_device)(struct udev_monitor *) = NIL;
46 static void (*ex_udev_monitor_unref)(struct udev_monitor *) = NIL;
47
48 static Void FreeLibrary()
49 {
50 if (loader != NIL)
51 {
52 delete loader;
53
54 loader = NIL;
55 }
56 }
57
58 static Bool LoadLibrary()
59 {
60 if (loader == NIL)
61 {
62 loader = new DynamicLoader("udev");
63
64 if (loader->GetSystemModuleHandle() == NIL) { FreeLibrary(); return False; }
65
66 ex_udev_new = (struct udev *(*)(void)) loader->GetFunctionAddress("udev_new");
67 ex_udev_unref = (void (*)(struct udev *)) loader->GetFunctionAddress("udev_unref");
68
69 ex_udev_device_get_devnode = (const char *(*)(struct udev_device *)) loader->GetFunctionAddress("udev_device_get_devnode");
70 ex_udev_device_get_property_value = (const char *(*)(struct udev_device *, const char *)) loader->GetFunctionAddress("udev_device_get_property_value");
71 ex_udev_device_unref = (void (*)(struct udev_device *)) loader->GetFunctionAddress("udev_device_unref");
72
73 ex_udev_monitor_new_from_netlink = (struct udev_monitor *(*)(struct udev *, const char *)) loader->GetFunctionAddress("udev_monitor_new_from_netlink");
74 ex_udev_monitor_filter_add_match_subsystem_devtype = (int (*)(struct udev_monitor *, const char *, const char *)) loader->GetFunctionAddress("udev_monitor_filter_add_match_subsystem_devtype");
75 ex_udev_monitor_enable_receiving = (int (*)(struct udev_monitor *)) loader->GetFunctionAddress("udev_monitor_enable_receiving");
76 ex_udev_monitor_get_fd = (int (*)(struct udev_monitor *)) loader->GetFunctionAddress("udev_monitor_get_fd");
77 ex_udev_monitor_receive_device = (struct udev_device *(*)(struct udev_monitor *)) loader->GetFunctionAddress("udev_monitor_receive_device");
78 ex_udev_monitor_unref = (void (*)(struct udev_monitor *)) loader->GetFunctionAddress("udev_monitor_unref");
79
80 if (ex_udev_new == NIL ||
81 ex_udev_unref == NIL ||
82 ex_udev_device_get_devnode == NIL ||
83 ex_udev_device_get_property_value == NIL ||
84 ex_udev_device_unref == NIL ||
85 ex_udev_monitor_new_from_netlink == NIL ||
86 ex_udev_monitor_filter_add_match_subsystem_devtype == NIL ||
87 ex_udev_monitor_enable_receiving == NIL ||
88 ex_udev_monitor_get_fd == NIL ||
89 ex_udev_monitor_receive_device == NIL ||
90 ex_udev_monitor_unref == NIL) { FreeLibrary(); return False; }
91 }
92
93 return True;
94 }
95
96 static Void CheckNotification()
97 {
98 /* Check for available notifications.
99 */
100 struct timeval timeval = { 0 };
101 fd_set fdset;
102
103 FD_ZERO(&fdset);
104 FD_SET(udevFd, &fdset);
105
106 if (select(udevFd + 1, &fdset, NIL, NIL, &timeval) <= 0) return;
107
108 /* Receive device notification.
109 */
110 struct udev_device *device = ex_udev_monitor_receive_device(udevMonitor);
111
112 if (device == NIL) return;
113
114 /* Look for the drive number using the device path.
115 */
116 Registry &boca = Registry::Get();
117 DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
118
119 if (info != NIL)
120 {
121 for (Int drive = 0; drive < info->GetNumberOfDevices(); drive++)
122 {
123 if (info->GetNthDeviceInfo(drive).path == ex_udev_device_get_devnode(device))
124 {
125 if (String(ex_udev_device_get_property_value(device, "ID_CDROM_MEDIA")) == "1") freac::Notification::Get()->onDiscInsert.Emit(drive);
126 else freac::Notification::Get()->onDiscRemove.Emit(drive);
127
128 break;
129 }
130 }
131
132 boca.DeleteComponent(info);
133 }
134
135 ex_udev_device_unref(device);
136 }
137
138 freac::Notification *freac::Notification::instance = NIL;
139
140 freac::Notification::Notification() : privateData(NIL)
141 {
142 /* Initialize udev library.
143 */
144 udev = ex_udev_new();
145
146 if (udev != NIL)
147 {
148 /* Setup udev monitor.
149 */
150 udevMonitor = ex_udev_monitor_new_from_netlink(udev, "udev");
151
152 if (udevMonitor != NIL)
153 {
154 ex_udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, "block", "disk");
155 ex_udev_monitor_enable_receiving(udevMonitor);
156
157 udevFd = ex_udev_monitor_get_fd(udevMonitor);
158
159 /* Create a timer to fire every half second.
160 */
161 privateData = new Timer();
162
163 ((Timer *) privateData)->onInterval.Connect(&CheckNotification);
164 ((Timer *) privateData)->Start(500);
165 }
166 }
167 }
168
169 freac::Notification::~Notification()
170 {
171 /* Stop timer.
172 */
173 if (privateData != NIL)
174 {
175 ((Timer *) privateData)->Stop();
176
177 Object::DeleteObject((Timer *) privateData);
178 }
179
180 /* Clean up udev monitor.
181 */
182 if (udevMonitor != NIL) ex_udev_monitor_unref(udevMonitor);
183 if (udev != NIL) ex_udev_unref(udev);
184 }
185
186 freac::Notification *freac::Notification::Get()
187 {
188 if (instance == NIL)
189 {
190 if (!LoadLibrary()) return NIL;
191
192 instance = new Notification();
193 }
194
195 return instance;
196 }
197
198 Void freac::Notification::Free()
199 {
200 if (instance != NIL)
201 {
202 delete instance;
203
204 instance = NIL;
205
206 FreeLibrary();
207 }
208 }
209
210 Bool freac::Notification::IsNotificationAvailable() const
211 {
212 return True;
213 }
214
215 Void freac::Notification::ProcessSystemMessage(Int message, Int wParam, Int lParam)
216 {
217 }