1 From 672a932784bd33f2b6397e80827dd1cdc3caabd7 Mon Sep 17 00:00:00 2001
2 From: Alexander Pyhalov <apyhalov@gmail.com>
3 Date: Thu, 20 Feb 2020 08:20:01 +0300
4 Subject: [PATCH] Revert "all: Remove HAL"
6 This reverts commit ce78295bbf068f8da27706666e693cbd007855cb.
9 doc/reference/Makefile.am | 1 +
10 lib/libmediaplayerid/Makefile.am | 15 ++-
11 lib/libmediaplayerid/mpid-device.c | 2 +-
12 lib/libmediaplayerid/mpid-util.c | 4 +-
13 plugins/mtpdevice/Makefile.am | 2 +
14 plugins/mtpdevice/rb-mtp-plugin.c | 195 +++++++++++++++++++++++++++++
15 7 files changed, 238 insertions(+), 6 deletions(-)
17 diff --git a/configure.ac b/configure.ac
18 index 9e1ffa42d..8b50b8410 100644
21 @@ -147,6 +147,25 @@ else
23 AM_CONDITIONAL(USE_GUDEV, test x"$use_gudev" = xyes)
27 + AC_HELP_STRING([--without-hal],
28 + [Disable HAL support]))
29 +if test "x$with_hal" != "xno"; then
30 + PKG_CHECK_MODULES(HAL, hal >= 0.5 hal < 0.6 dbus-glib-1, enable_hal=yes, enable_hal=no)
31 + if test "x$enable_hal" != "xyes" -a "x$with_hal" = "xyes"; then
32 + AC_MSG_ERROR([HAL support explicitly requested but HAL couldn't be found])
35 + if test "x$enable_hal" = "xyes"; then
36 + AC_DEFINE(HAVE_HAL, 1, [Define if you have HAL support])
37 + AC_SUBST(HAL_CFLAGS)
41 +AM_CONDITIONAL(HAVE_HAL, test x"$enable_hal" = xyes)
46 AC_HELP_STRING([--with-ipod],
47 @@ -178,7 +197,7 @@ AC_ARG_WITH(mtp,
49 if test "x$with_mtp" != "xno"; then
51 - if test x"$use_gudev" = "xyes"; then
52 + if test x"$use_gudev" = "xyes" -o x"$enable_hal" = "xyes"; then
56 @@ -188,7 +207,7 @@ if test "x$with_mtp" != "xno"; then
57 AC_MSG_ERROR([MTP explicitly requested but libmtp is not available])
59 if test x"$can_use_mtp" = "xno"; then
60 - AC_MSG_ERROR([MTP explicitly requested but GUdev is not available])
61 + AC_MSG_ERROR([MTP explicitly requested but GUdev and HAL are not available])
64 if test "x$have_libmtp" = "xyes" -a "x$can_use_mtp" = "xyes"; then
65 @@ -773,6 +792,8 @@ else
67 if test x"$use_gudev" = xyes; then
68 AC_MSG_NOTICE([** GUdev support enabled])
69 +elif test x"$enable_hal" = xyes; then
70 + AC_MSG_NOTICE([** HAL support enabled])
72 AC_MSG_NOTICE([ GUdev support disabled])
74 diff --git a/doc/reference/Makefile.am b/doc/reference/Makefile.am
75 index 5d3bb57c1..28678e5ca 100644
76 --- a/doc/reference/Makefile.am
77 +++ b/doc/reference/Makefile.am
78 @@ -186,6 +186,7 @@ GTKDOC_LIBS=\
79 $(top_builddir)/shell/librhythmbox-core.la \
81 $(TOTEM_PLPARSER_LIBS) \
86 diff --git a/lib/libmediaplayerid/Makefile.am b/lib/libmediaplayerid/Makefile.am
87 index 2ccc6f90a..cda6e5fc3 100644
88 --- a/lib/libmediaplayerid/Makefile.am
89 +++ b/lib/libmediaplayerid/Makefile.am
90 @@ -16,7 +16,8 @@ AM_CPPFLAGS = \
94 -# use the GUdev/media-player-id implementation if possible.
95 +# use the GUdev/media-player-id implementation if possible,
100 @@ -28,8 +29,20 @@ libmediaplayerid_la_LIBADD = $(GUDEV_LIBS)
106 +libmediaplayerid_la_SOURCES += mpid-hal.c
107 +EXTRA_DIST = mpid-udev.c mpid-dummy.c
109 +AM_CPPFLAGS += $(HAL_CFLAGS)
110 +libmediaplayerid_la_LIBADD = $(HAL_LIBS)
114 libmediaplayerid_la_SOURCES += mpid-dummy.c
115 EXTRA_DIST = mpid-udev.c mpid-hal.c
121 diff --git a/lib/libmediaplayerid/mpid-device.c b/lib/libmediaplayerid/mpid-device.c
122 index 7caf34d11..d0b8e2a59 100644
123 --- a/lib/libmediaplayerid/mpid-device.c
124 +++ b/lib/libmediaplayerid/mpid-device.c
127 * MPID provides access to device information, such as device and vendor names,
128 * supported formats, and audio folder locations, for USB mass storage media
129 - * player devices. It queries the operating system (udev) and reads
130 + * player devices. It queries the operating system (udev or HAL) and reads
131 * override files from the device filesystem and provides a simple set of
134 diff --git a/lib/libmediaplayerid/mpid-util.c b/lib/libmediaplayerid/mpid-util.c
135 index b5bfd0dd9..5a821d5aa 100644
136 --- a/lib/libmediaplayerid/mpid-util.c
137 +++ b/lib/libmediaplayerid/mpid-util.c
138 @@ -31,7 +31,7 @@ static gboolean debug_enabled = FALSE;
140 * @MPID_ERROR_NONE: Indicates no error has occurred
141 * @MPID_ERROR_NO_DEVICE_PATH: Unable to find the device path
142 - * @MPID_ERROR_MECHANISM_FAILED: The device detection mechanism (e.g. udev) failed
143 + * @MPID_ERROR_MECHANISM_FAILED: The device detection mechanism (e.g. udev or HAL) failed
144 * @MPID_ERROR_NOT_MEDIA_PLAYER: The device is not a media player
145 * @MPID_ERROR_DEVICE_INFO_MISSING: The device detection mechanism identified the device
146 * but was unable to locate its device information
147 @@ -61,7 +61,7 @@ mpid_error_get_type (void)
150 * @MPID_SOURCE_NONE: No device information is available
151 - * @MPID_SOURCE_SYSTEM: Device information provided by the operating system (e.g. udev)
152 + * @MPID_SOURCE_SYSTEM: Device information provided by the operating system (e.g. udev or HAL)
153 * @MPID_SOURCE_OVERRIDE: Device information provided by an override file on the device itself.
156 diff --git a/plugins/mtpdevice/Makefile.am b/plugins/mtpdevice/Makefile.am
157 index b860cb696..cac9402c6 100644
158 --- a/plugins/mtpdevice/Makefile.am
159 +++ b/plugins/mtpdevice/Makefile.am
160 @@ -20,6 +20,7 @@ libmtpdevice_la_LIBTOOLFLAGS = --tag=disable-static
161 libmtpdevice_la_LIBADD = \
162 $(top_builddir)/shell/librhythmbox-core.la \
168 @@ -42,6 +43,7 @@ AM_CPPFLAGS = \
169 -DDATADIR=\""$(datadir)"\" \
170 $(RHYTHMBOX_CFLAGS) \
175 plugin_in_files = mtpdevice.plugin.in
176 diff --git a/plugins/mtpdevice/rb-mtp-plugin.c b/plugins/mtpdevice/rb-mtp-plugin.c
177 index 7e1d7fc82..52ecb083b 100644
178 --- a/plugins/mtpdevice/rb-mtp-plugin.c
179 +++ b/plugins/mtpdevice/rb-mtp-plugin.c
181 #include <glib-object.h>
184 +#if defined(HAVE_GUDEV)
185 #define G_UDEV_API_IS_SUBJECT_TO_CHANGE
186 #include <gudev/gudev.h>
188 +#include <hal/libhal.h>
189 +#include <dbus/dbus.h>
190 +#include <dbus/dbus-glib.h>
191 +#include <dbus/dbus-glib-lowlevel.h>
194 #include "rb-plugin-macros.h"
195 #include "rb-source.h"
196 @@ -70,6 +77,11 @@ typedef struct
197 guint create_device_source_id;
201 +#if !defined(HAVE_GUDEV)
202 + LibHalContext *hal_context;
203 + DBusConnection *dbus_connection;
208 @@ -82,7 +94,14 @@ G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);
210 static void rb_mtp_plugin_init (RBMtpPlugin *plugin);
212 +#if defined(HAVE_GUDEV)
213 static RBSource* create_source_device_cb (RBRemovableMediaManager *rmm, GObject *device, RBMtpPlugin *plugin);
215 +static void rb_mtp_plugin_maybe_add_source (RBMtpPlugin *plugin, const char *udi, LIBMTP_raw_device_t *raw_devices, int num);
216 +static void rb_mtp_plugin_device_added (LibHalContext *context, const char *udi);
217 +static void rb_mtp_plugin_device_removed (LibHalContext *context, const char *udi);
218 +static gboolean rb_mtp_plugin_setup_dbus_hal_connection (RBMtpPlugin *plugin);
221 GType rb_mtp_src_get_type (void);
222 GType rb_mtp_sink_get_type (void);
223 @@ -102,13 +121,19 @@ impl_activate (PeasActivatable *bplugin)
224 RBMtpPlugin *plugin = RB_MTP_PLUGIN (bplugin);
225 RBRemovableMediaManager *rmm;
227 +#if defined(HAVE_GUDEV)
228 gboolean rmm_scanned = FALSE;
230 + int num_mtp_devices;
231 + LIBMTP_raw_device_t *mtp_devices;
234 g_object_get (plugin, "object", &shell, NULL);
235 g_object_get (shell, "removable-media-manager", &rmm, NULL);
236 g_object_unref (shell);
238 /* device detection */
239 +#if defined(HAVE_GUDEV)
240 plugin->create_device_source_id =
241 g_signal_connect_object (rmm,
242 "create-source-device",
243 @@ -120,6 +145,34 @@ impl_activate (PeasActivatable *bplugin)
244 g_object_get (rmm, "scanned", &rmm_scanned, NULL);
246 rb_removable_media_manager_scan (rmm);
248 + if (rb_mtp_plugin_setup_dbus_hal_connection (plugin) == FALSE) {
249 + rb_debug ("not scanning for MTP devices because we couldn't get a HAL context");
250 + g_object_unref (rmm);
254 + rb_profile_start ("scanning for MTP devices");
255 + LIBMTP_Detect_Raw_Devices (&mtp_devices, &num_mtp_devices);
256 + if (num_mtp_devices > 0) {
257 + int num_hal_devices;
258 + char **hal_devices;
261 + rb_debug ("%d MTP devices found", num_mtp_devices);
263 + hal_devices = libhal_get_all_devices (plugin->hal_context, &num_hal_devices, NULL);
264 + for (i = 0; i < num_hal_devices; i++) {
265 + /* should narrow this down a bit - usb only, for a start */
266 + rb_mtp_plugin_maybe_add_source (plugin, hal_devices[i], mtp_devices, num_mtp_devices);
268 + libhal_free_string_array (hal_devices);
270 + if (mtp_devices != NULL) {
271 + free (mtp_devices);
273 + rb_profile_end ("scanning for MTP devices");
276 g_object_unref (rmm);
278 @@ -140,13 +193,31 @@ impl_deactivate (PeasActivatable *bplugin)
279 g_list_free (plugin->mtp_sources);
280 plugin->mtp_sources = NULL;
282 +#if defined(HAVE_GUDEV)
283 g_signal_handler_disconnect (rmm, plugin->create_device_source_id);
284 plugin->create_device_source_id = 0;
286 + if (plugin->hal_context != NULL) {
288 + dbus_error_init (&error);
289 + libhal_ctx_shutdown (plugin->hal_context, &error);
290 + libhal_ctx_free (plugin->hal_context);
291 + dbus_error_free (&error);
293 + plugin->hal_context = NULL;
296 + if (plugin->dbus_connection != NULL) {
297 + dbus_connection_unref (plugin->dbus_connection);
298 + plugin->dbus_connection = NULL;
302 g_object_unref (rmm);
303 g_object_unref (shell);
306 +#if defined(HAVE_GUDEV)
308 source_deleted_cb (RBMtpSource *source, RBMtpPlugin *plugin)
310 @@ -239,6 +310,130 @@ create_source_device_cb (RBRemovableMediaManager *rmm, GObject *device_obj, RBMt
317 +source_deleted_cb (RBMtpSource *source, RBMtpPlugin *plugin)
319 + plugin->mtp_sources = g_list_remove (plugin->mtp_sources, source);
323 +rb_mtp_plugin_maybe_add_source (RBMtpPlugin *plugin, const char *udi, LIBMTP_raw_device_t *raw_devices, int num_raw_devices)
326 + int device_num = 0;
329 + rb_debug ("checking if UDI %s matches an MTP device", udi);
331 + /* get device number */
332 + dbus_error_init (&error);
333 + device_num = libhal_device_get_property_int (plugin->hal_context, udi, "usb.linux.device_number", &error);
334 + if (dbus_error_is_set (&error)) {
335 + rb_debug ("unable to get USB device number: %s", error.message);
336 + dbus_error_free (&error);
340 + rb_debug ("USB device number: %d", device_num);
342 + for (i = 0; i < num_raw_devices; i++) {
343 + rb_debug ("detected MTP device: device number %d (bus location %u)", raw_devices[i].devnum, raw_devices[i].bus_location);
344 + if (raw_devices[i].devnum == device_num) {
348 + rb_debug ("device matched, creating a source");
349 + g_object_get (plugin, "object", &shell, NULL);
350 + source = RB_SOURCE (rb_mtp_source_new (shell, G_OBJECT (plugin), udi, &raw_devices[i]));
352 + rb_shell_append_display_page (shell, RB_DISPLAY_PAGE (source), RB_DISPLAY_PAGE_GROUP_DEVICES);
353 + plugin->mtp_sources = g_list_prepend (plugin->mtp_sources, source);
354 + g_signal_connect_object (source,
355 + "deleted", G_CALLBACK (source_deleted_cb),
357 + g_object_unref (shell);
363 +rb_mtp_plugin_device_added (LibHalContext *context, const char *udi)
365 + RBMtpPlugin *plugin = (RBMtpPlugin *) libhal_ctx_get_user_data (context);
366 + LIBMTP_raw_device_t *mtp_devices;
367 + int num_mtp_devices;
369 + LIBMTP_Detect_Raw_Devices (&mtp_devices, &num_mtp_devices);
370 + if (mtp_devices != NULL) {
371 + rb_mtp_plugin_maybe_add_source (plugin, udi, mtp_devices, num_mtp_devices);
372 + free (mtp_devices);
377 +rb_mtp_plugin_device_removed (LibHalContext *context, const char *udi)
379 + RBMtpPlugin *plugin = (RBMtpPlugin *) libhal_ctx_get_user_data (context);
380 + GList *list = plugin->mtp_sources;
383 + for (tmp = list; tmp != NULL; tmp = tmp->next) {
384 + RBSource *source = (RBSource *)tmp->data;
387 + g_object_get (source, "udi", &source_udi, NULL);
388 + if (strcmp (udi, source_udi) == 0) {
389 + rb_debug ("removing device %s, %p", udi, source);
390 + plugin->mtp_sources = g_list_remove (plugin->mtp_sources, source);
391 + rb_display_page_delete_thyself (RB_DISPLAY_PAGE (source));
393 + g_free (source_udi);
398 +rb_mtp_plugin_setup_dbus_hal_connection (RBMtpPlugin *plugin)
402 + dbus_error_init (&error);
403 + plugin->dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
404 + if (plugin->dbus_connection == NULL) {
405 + rb_debug ("error: dbus_bus_get: %s: %s\n", error.name, error.message);
406 + dbus_error_free (&error);
410 + dbus_connection_setup_with_g_main (plugin->dbus_connection, NULL);
412 + rb_debug ("connected to: %s", dbus_bus_get_unique_name (plugin->dbus_connection));
414 + plugin->hal_context = libhal_ctx_new ();
415 + if (plugin->hal_context == NULL) {
416 + dbus_error_free (&error);
419 + libhal_ctx_set_dbus_connection (plugin->hal_context, plugin->dbus_connection);
421 + libhal_ctx_set_user_data (plugin->hal_context, (void *)plugin);
422 + libhal_ctx_set_device_added (plugin->hal_context, rb_mtp_plugin_device_added);
423 + libhal_ctx_set_device_removed (plugin->hal_context, rb_mtp_plugin_device_removed);
424 + libhal_device_property_watch_all (plugin->hal_context, &error);
426 + if (!libhal_ctx_init (plugin->hal_context, &error)) {
427 + rb_debug ("error: libhal_ctx_init: %s: %s\n", error.name, error.message);
428 + dbus_error_free (&error);
432 + dbus_error_free (&error);
439 peas_register_types (PeasObjectModule *module)