static void setup_hal_devices (void) { DBusConnection *connection; DBusError error; LibHalContext *ctx; char **devices; int i, num = 0; dbus_error_init (&error); connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (connection == NULL) { /* cannot get a dbus connection */ if (dbus_error_is_set (&error)) { g_warning ("Getting a system dbus connection an error occured: %s", error.message); dbus_error_free (&error); } return; } dbus_connection_setup_with_g_main (connection, g_main_context_default ()); ctx = libhal_ctx_new (); g_return_if_fail (ctx != NULL); libhal_ctx_set_device_added (ctx, device_added_callback); libhal_ctx_set_device_removed (ctx, device_removed_callback); libhal_ctx_set_dbus_connection (ctx, connection); if (!libhal_ctx_init (ctx, &error)) { /* cannot connect to hald */ if (dbus_error_is_set (&error)) { g_warning ("Connecting to hald an error occured: %s", error.message); dbus_error_free (&error); } return; } devices = libhal_find_device_by_capability (ctx, "alsa", &num, &error); if (devices == NULL) { /* error in the libhal_find_device_by_capability function */ if (dbus_error_is_set (&error)) { g_warning ("Calling a hal function an error occured: %s", error.message); dbus_error_free (&error); } return; } for (i = 0; i < num; i++) { device_added_callback (ctx, devices[i]); } dbus_free_string_array (devices); }
static gboolean rb_mtp_plugin_setup_dbus_hal_connection (RBMtpPlugin *plugin) { DBusError error; dbus_error_init (&error); plugin->dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (plugin->dbus_connection == NULL) { rb_debug ("error: dbus_bus_get: %s: %s\n", error.name, error.message); dbus_error_free (&error); return FALSE; } dbus_connection_setup_with_g_main (plugin->dbus_connection, NULL); rb_debug ("connected to: %s", dbus_bus_get_unique_name (plugin->dbus_connection)); plugin->hal_context = libhal_ctx_new (); if (plugin->hal_context == NULL) { dbus_error_free (&error); return FALSE; } libhal_ctx_set_dbus_connection (plugin->hal_context, plugin->dbus_connection); libhal_ctx_set_user_data (plugin->hal_context, (void *)plugin); libhal_ctx_set_device_added (plugin->hal_context, rb_mtp_plugin_device_added); libhal_ctx_set_device_removed (plugin->hal_context, rb_mtp_plugin_device_removed); libhal_device_property_watch_all (plugin->hal_context, &error); if (!libhal_ctx_init (plugin->hal_context, &error)) { rb_debug ("error: libhal_ctx_init: %s: %s\n", error.name, error.message); dbus_error_free (&error); return FALSE; } dbus_error_free (&error); return TRUE; }
static int init_hal(void) { DBusError error; if (!(hal_ctx = libhal_ctx_new())) return -1; libhal_ctx_set_dbus_connection(hal_ctx, dbus_conn); libhal_ctx_set_device_added(hal_ctx, device_added); libhal_ctx_set_device_removed(hal_ctx, device_removed); dbus_error_init(&error); if (!libhal_device_property_watch_all(hal_ctx, &error)) { if (dbus_error_is_set(&error)) { syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__, error.name, error.message); dbus_error_free (&error); libhal_ctx_free (hal_ctx); return -1; } } if (!libhal_ctx_init(hal_ctx, &error)) { if (dbus_error_is_set(&error)) { syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__, error.name, error.message); dbus_error_free(&error); libhal_ctx_free(hal_ctx); return -1; } } return 0; }
static BOOL connect_and_register(DBusConnection *connection, struct config_hal_info *info) { DBusError error; char **devices; int num_devices, i; if (info->hal_ctx) return TRUE; /* already registered, pretend we did something */ info->system_bus = connection; dbus_error_init(&error); info->hal_ctx = libhal_ctx_new(); if (!info->hal_ctx) { LogMessage(X_ERROR, "config/hal: couldn't create HAL context\n"); goto out_err; } if (!libhal_ctx_set_dbus_connection(info->hal_ctx, info->system_bus)) { LogMessage(X_ERROR, "config/hal: couldn't associate HAL context with bus\n"); goto out_err; } if (!libhal_ctx_init(info->hal_ctx, &error)) { LogMessage(X_ERROR, "config/hal: couldn't initialise context: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); goto out_err; } if (!libhal_device_property_watch_all(info->hal_ctx, &error)) { LogMessage(X_ERROR, "config/hal: couldn't watch all properties: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); goto out_ctx; } libhal_ctx_set_device_added(info->hal_ctx, device_added); libhal_ctx_set_device_removed(info->hal_ctx, device_removed); devices = libhal_find_device_by_capability(info->hal_ctx, "input", &num_devices, &error); /* FIXME: Get default devices if error is set. */ if (dbus_error_is_set(&error)) { LogMessage(X_ERROR, "config/hal: couldn't find input device: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); goto out_ctx; } for (i = 0; i < num_devices; i++) device_added(info->hal_ctx, devices[i]); libhal_free_string_array(devices); dbus_error_free(&error); return TRUE; out_ctx: dbus_error_free(&error); if (!libhal_ctx_shutdown(info->hal_ctx, &error)) { LogMessage(X_WARNING, "config/hal: couldn't shut down context: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); dbus_error_free(&error); } out_err: dbus_error_free(&error); if (info->hal_ctx) { libhal_ctx_free(info->hal_ctx); } info->hal_ctx = NULL; info->system_bus = NULL; return FALSE; }
// Initialize basic HAL connection LibHalContext *CHALManager::InitializeHal() { LibHalContext *ctx; char **devices; int nr; if (!InitializeDBus()) return NULL; if (!(ctx = libhal_ctx_new())) { CLog::Log(LOGERROR, "HAL: failed to create a HAL context!"); return NULL; } if (!libhal_ctx_set_dbus_connection(ctx, m_DBusSystemConnection)) CLog::Log(LOGERROR, "HAL: Failed to connect with dbus"); libhal_ctx_set_device_added(ctx, DeviceAdded); libhal_ctx_set_device_removed(ctx, DeviceRemoved); libhal_ctx_set_device_new_capability(ctx, DeviceNewCapability); libhal_ctx_set_device_lost_capability(ctx, DeviceLostCapability); libhal_ctx_set_device_property_modified(ctx, DevicePropertyModified); libhal_ctx_set_device_condition(ctx, DeviceCondition); if (!libhal_device_property_watch_all(ctx, &m_Error)) { CLog::Log(LOGERROR, "HAL: Failed to set property watch %s", m_Error.message); dbus_error_free(&m_Error); libhal_ctx_free(ctx); return NULL; } if (!libhal_ctx_init(ctx, &m_Error)) { CLog::Log(LOGERROR, "HAL: Failed to initialize hal context: %s", m_Error.message); dbus_error_free(&m_Error); libhal_ctx_free(ctx); return NULL; } /* * Do something to ping the HAL daemon - the above functions will * succeed even if hald is not running, so long as DBUS is. But we * want to exit silently if hald is not running, to behave on * pre-2.6 systems. */ if (!(devices = libhal_get_all_devices(ctx, &nr, &m_Error))) { CLog::Log(LOGERROR, "HAL: seems that Hal daemon is not running: %s", m_Error.message); dbus_error_free(&m_Error); libhal_ctx_shutdown(ctx, NULL); libhal_ctx_free(ctx); return NULL; } libhal_free_string_array(devices); return ctx; }
FormatDialog* format_dialog_new(void) { FormatDialog *dialog; dialog = g_new0 (FormatDialog, 1); const char* xmlpath = GLADEDIR "/gformat.glade"; dialog->xml = glade_xml_new (xmlpath, "toplevel", NULL); /* Try uninstalled next */ if (!dialog->xml) { xmlpath = "./gformat.glade"; dialog->xml = glade_xml_new (xmlpath, "toplevel", NULL); } if (dialog->xml == NULL){ GtkWidget *f_dialog; f_dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Glade file for the format program is missing.\n" "Please check your installation of gnome-utils"); gtk_dialog_run (GTK_DIALOG (f_dialog)); gtk_widget_destroy (f_dialog); exit (1); } /* Grab some widgets */ dialog->toplevel = glade_xml_get_widget (dialog->xml, "toplevel"); dialog->volume_combo = GTK_COMBO_BOX(glade_xml_get_widget(dialog->xml, "volume_combo")); dialog->show_partitions = GTK_TOGGLE_BUTTON(glade_xml_get_widget(dialog->xml, "show_partitions")); dialog->fs_combo = GTK_COMBO_BOX(glade_xml_get_widget(dialog->xml, "fs_combo")); dialog->extra_volume_info = GTK_LABEL(glade_xml_get_widget(dialog->xml, "extra_volume_info")); dialog->extra_volume_hbox = GTK_HBOX(glade_xml_get_widget(dialog->xml, "extra_volume_hbox")); dialog->progress_bar = GTK_PROGRESS_BAR(glade_xml_get_widget(dialog->xml, "progress_bar")); dialog->format_button = GTK_BUTTON(glade_xml_get_widget(dialog->xml, "format_button")); dialog->cancel_button = GTK_BUTTON(glade_xml_get_widget(dialog->xml, "cancel_button")); dialog->luks_subwindow = GTK_BOX(glade_xml_get_widget (dialog->xml, "luks_subwindow")); dialog->floppy_subwindow = GTK_BOX(glade_xml_get_widget (dialog->xml, "floppy_subwindow")); g_assert(dialog->toplevel != NULL); /* Get a HAL context; if we can't, bail */ dialog->hal_context = libhal_context_alloc(); glade_xml_signal_autoconnect(dialog->xml); g_object_set_data(G_OBJECT(dialog->toplevel), "userdata", dialog); /* Set stuff in the dialog up */ setup_volume_treeview(dialog); setup_filesystem_menu(dialog); gtk_widget_show_all (dialog->toplevel); update_dialog(dialog); /* We do this here so they at least see the window before we die */ if( !dialog->hal_context ) { show_error_dialog(dialog->toplevel, _("Cannot get list of disks"), _("Make sure the HAL daemon is running and configured correctly")); return NULL; } /* Get the HAL version and register the HAL device callbacks */ get_hal_version(dialog); g_debug("Registering callback!"); libhal_ctx_set_user_data(dialog->hal_context, dialog); if (libhal_ctx_set_device_added(dialog->hal_context, on_libhal_device_added_removed) == TRUE) printf("libhal_ctx_set_device_added called"); libhal_ctx_set_device_removed(dialog->hal_context, on_libhal_device_added_removed); libhal_ctx_set_device_property_modified(dialog->hal_context, on_libhal_prop_modified); libhal_ctx_set_device_new_capability(dialog->hal_context, NULL); libhal_ctx_set_device_lost_capability(dialog->hal_context, NULL); libhal_ctx_set_device_condition(dialog->hal_context, NULL); return dialog; }
char * battstat_hal_initialise (void (*callback) (void)) { DBusConnection *connection; LibHalContext *ctx; DBusError error; char *error_str; char **devices; int i, num; status_updated_callback = callback; if( battstat_hal_ctx != NULL ) return g_strdup( "Already initialised!" ); dbus_error_init( &error ); if( (connection = dbus_bus_get( DBUS_BUS_SYSTEM, &error )) == NULL ) goto error_out; dbus_connection_setup_with_g_main( connection, g_main_context_default() ); if( (ctx = libhal_ctx_new()) == NULL ) { dbus_set_error( &error, _("HAL error"), _("Could not create libhal_ctx") ); goto error_out; } libhal_ctx_set_device_property_modified( ctx, property_callback ); libhal_ctx_set_device_added( ctx, device_added_callback ); libhal_ctx_set_device_removed( ctx, device_removed_callback ); libhal_ctx_set_dbus_connection( ctx, connection ); if( libhal_ctx_init( ctx, &error ) == 0 ) goto error_freectx; devices = libhal_find_device_by_capability( ctx, "battery", &num, &error ); if( devices == NULL ) goto error_shutdownctx; /* FIXME: for now, if 0 battery devices are present on first scan, then fail. * This allows fallover to the legacy (ACPI, APM, etc) backends if the * installed version of HAL doesn't know about batteries. This check should * be removed at some point in the future (maybe circa MATE 2.13..). */ if( num == 0 ) { dbus_free_string_array( devices ); dbus_set_error( &error, _("HAL error"), _("No batteries found") ); goto error_shutdownctx; } for( i = 0; i < num; i++ ) { char *type = libhal_device_get_property_string( ctx, devices[i], "battery.type", &error ); if( type ) { /* We only track 'primary' batteries (ie: to avoid monitoring * batteries in cordless mice or UPSes etc.) */ if( !strcmp( type, "primary" ) ) add_to_list( ctx, &batteries, devices[i], sizeof (struct battery_info) ); libhal_free_string( type ); } } dbus_free_string_array( devices ); devices = libhal_find_device_by_capability( ctx, "ac_adapter", &num, &error ); if( devices == NULL ) { batteries = free_entire_list( batteries ); goto error_shutdownctx; } for( i = 0; i < num; i++ ) add_to_list( ctx, &adaptors, devices[i], sizeof (struct adaptor_info) ); dbus_free_string_array( devices ); dbus_error_free( &error ); battstat_hal_ctx = ctx; return NULL; error_shutdownctx: libhal_ctx_shutdown( ctx, NULL ); error_freectx: libhal_ctx_free( ctx ); error_out: error_str = g_strdup_printf( _("Unable to initialise HAL: %s: %s"), error.name, error.message ); dbus_error_free( &error ); return error_str; }
static void thunar_vfs_volume_manager_hal_init (ThunarVfsVolumeManagerHal *manager_hal) { LibHalDrive *hd; DBusError error; gchar **drive_udis; gchar **udis; gint n_drive_udis; gint n_udis; gint n, m; /* initialize the D-BUS error */ dbus_error_init (&error); /* allocate a HAL context */ manager_hal->context = libhal_ctx_new (); if (G_UNLIKELY (manager_hal->context == NULL)) return; /* try to connect to the system bus */ manager_hal->dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (G_UNLIKELY (manager_hal->dbus_connection == NULL)) goto failed; /* setup the D-BUS connection for the HAL context */ libhal_ctx_set_dbus_connection (manager_hal->context, manager_hal->dbus_connection); /* connect our manager object to the HAL context */ libhal_ctx_set_user_data (manager_hal->context, manager_hal); /* setup callbacks */ libhal_ctx_set_device_added (manager_hal->context, thunar_vfs_volume_manager_hal_device_added); libhal_ctx_set_device_removed (manager_hal->context, thunar_vfs_volume_manager_hal_device_removed); libhal_ctx_set_device_new_capability (manager_hal->context, thunar_vfs_volume_manager_hal_device_new_capability); libhal_ctx_set_device_lost_capability (manager_hal->context, thunar_vfs_volume_manager_hal_device_lost_capability); libhal_ctx_set_device_property_modified (manager_hal->context, thunar_vfs_volume_manager_hal_device_property_modified); libhal_ctx_set_device_condition (manager_hal->context, thunar_vfs_volume_manager_hal_device_condition); /* try to initialize the HAL context */ if (!libhal_ctx_init (manager_hal->context, &error)) goto failed; /* setup the D-BUS connection with the GLib main loop */ dbus_connection_setup_with_g_main (manager_hal->dbus_connection, NULL); /* lookup all drives currently known to HAL */ drive_udis = libhal_find_device_by_capability (manager_hal->context, "storage", &n_drive_udis, &error); if (G_LIKELY (drive_udis != NULL)) { /* process all drives UDIs */ for (m = 0; m < n_drive_udis; ++m) { /* determine the LibHalDrive for the drive UDI */ hd = libhal_drive_from_udi (manager_hal->context, drive_udis[m]); if (G_UNLIKELY (hd == NULL)) continue; /* check if we have a floppy disk here */ if (libhal_drive_get_type (hd) == LIBHAL_DRIVE_TYPE_FLOPPY) { /* add the drive based on the UDI */ thunar_vfs_volume_manager_hal_device_added (manager_hal->context, drive_udis[m]); } else { /* determine all volumes for the given drive */ udis = libhal_drive_find_all_volumes (manager_hal->context, hd, &n_udis); if (G_LIKELY (udis != NULL)) { /* add volumes for all given UDIs */ for (n = 0; n < n_udis; ++n) { /* add the volume based on the UDI */ thunar_vfs_volume_manager_hal_device_added (manager_hal->context, udis[n]); /* release the UDI (HAL bug #5279) */ free (udis[n]); } /* release the UDIs array (HAL bug #5279) */ free (udis); } } /* release the hal drive */ libhal_drive_free (hd); } /* release the drive UDIs */ libhal_free_string_array (drive_udis); } /* watch all devices for changes */ if (!libhal_device_property_watch_all (manager_hal->context, &error)) goto failed; return; failed: /* release the HAL context */ if (G_LIKELY (manager_hal->context != NULL)) { libhal_ctx_free (manager_hal->context); manager_hal->context = NULL; } /* print a warning message */ if (dbus_error_is_set (&error)) { g_warning (_("Failed to connect to the HAL daemon: %s"), error.message); dbus_error_free (&error); } }
static void connect_hook(DBusConnection *connection, void *data) { DBusError error; struct config_hal_info *info = data; char **devices; int num_devices, i; info->system_bus = connection; dbus_error_init(&error); if (!info->hal_ctx) info->hal_ctx = libhal_ctx_new(); if (!info->hal_ctx) { ErrorF("[config/hal] couldn't create HAL context\n"); goto out_err; } if (!libhal_ctx_set_dbus_connection(info->hal_ctx, info->system_bus)) { ErrorF("[config/hal] couldn't associate HAL context with bus\n"); goto out_ctx; } if (!libhal_ctx_init(info->hal_ctx, &error)) { ErrorF("[config/hal] couldn't initialise context: %s (%s)\n", error.name, error.message); goto out_ctx; } libhal_ctx_set_device_added(info->hal_ctx, device_added); libhal_ctx_set_device_removed(info->hal_ctx, device_removed); if (!libhal_device_property_watch_all(info->hal_ctx, &error)) { ErrorF("[config/hal] couldn't watch all properties: %s (%s)\n", error.name, error.message); goto out_ctx2; } devices = libhal_find_device_by_capability(info->hal_ctx, "input", &num_devices, &error); /* FIXME: Get default devices if error is set. */ for (i = 0; i < num_devices; i++) device_added(info->hal_ctx, devices[i]); libhal_free_string_array(devices); dbus_error_free(&error); return; out_ctx2: if (!libhal_ctx_shutdown(info->hal_ctx, &error)) DebugF("[config/hal] couldn't shut down context: %s (%s)\n", error.name, error.message); out_ctx: libhal_ctx_free(info->hal_ctx); out_err: dbus_error_free(&error); info->hal_ctx = NULL; info->system_bus = NULL; return; }
static void init_hal (OdccmDeviceManager *self) { OdccmDeviceManagerPrivate *priv = ODCCM_DEVICE_MANAGER_GET_PRIVATE (self); gboolean success = FALSE, initialized = FALSE; DBusConnection *conn; const gchar *func_name = ""; DBusError error; dbus_error_init (&error); if ((conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error)) == NULL) { func_name = "dbus_bus_get"; goto DBUS_ERROR; } dbus_connection_setup_with_g_main (conn, NULL); priv->hal_ctx = libhal_ctx_new (); if (priv->hal_ctx == NULL) goto OUT; if (!libhal_ctx_set_dbus_connection (priv->hal_ctx, conn)) goto OUT; if (!libhal_ctx_init (priv->hal_ctx, &error)) { func_name = "libhal_ctx_init"; goto DBUS_ERROR; } initialized = TRUE; if (!libhal_ctx_set_user_data (priv->hal_ctx, self)) goto OUT; if (!libhal_ctx_set_device_added (priv->hal_ctx, hal_device_added_cb)) goto OUT; if (!libhal_ctx_set_device_removed (priv->hal_ctx, hal_device_removed_cb)) goto OUT; success = TRUE; goto OUT; DBUS_ERROR: g_warning ("%s failed with D-Bus error %s: %s\n", func_name, error.name, error.message); dbus_error_free (&error); OUT: if (!success) { if (!initialized) { libhal_ctx_free (priv->hal_ctx); priv->hal_ctx = NULL; } g_warning ("%s: failed", G_STRFUNC); } }