// 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 int init_hal(void) { LibHalContext *ctx; DBusError error; DBusConnection *dbus_connection; char **devices, **volumes; int i, num; if (!(ctx = libhal_ctx_new())) { fprintf(stderr, "can't initialize\n"); return -1; } dbus_error_init(&error); dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); if (dbus_error_is_set(&error)) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); return -1; } dbus_connection_setup_with_g_main(dbus_connection, NULL); libhal_ctx_set_dbus_connection(ctx, dbus_connection); libhal_ctx_set_device_property_modified(ctx, hal_property_modified); if (!libhal_device_property_watch_all(ctx, &error)) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); libhal_ctx_free(ctx); return -1; } if (!libhal_ctx_init(ctx, &error)) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); libhal_ctx_free(ctx); return -1; } if (!(devices = libhal_get_all_devices(ctx, &num, &error))) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); libhal_ctx_shutdown(ctx, NULL); libhal_ctx_free(ctx); return -1; } libhal_free_string_array(devices); volumes = libhal_find_device_by_capability(ctx, "volume", &num, &error); if (dbus_error_is_set(&error)) { printf("can't find volume devices: %s\n", error.message); dbus_error_free(&error); libhal_ctx_shutdown(ctx, NULL); libhal_ctx_free(ctx); return -1; } for (i = 0; i < num; i++) { char *udi = volumes[i]; if (libhal_device_property_exists(ctx, udi, "volume.is_mounted", NULL) && libhal_device_get_property_bool(ctx, udi, "volume.is_mounted", NULL)) { hal_property_modified(ctx, udi, "volume.is_mounted", FALSE, FALSE); hal_property_modified(ctx, udi, "volume.mount_point", FALSE, FALSE); } } libhal_free_string_array(volumes); return 0; }
int main (int argc, char *argv[]) { DBusError err; int retval = 0; hal_set_proc_title_init (argc, argv); setup_logger (); device_udi = getenv ("UDI"); HAL_DEBUG (("device:[%s]", device_udi)); if (device_udi == NULL) { HAL_ERROR (("No device specified")); return -2; } dbus_error_init (&err); if ((halctx = libhal_ctx_init_direct (&err)) == NULL) { HAL_ERROR (("Cannot connect to hald")); retval = -3; goto out; } /* update_properties */ libhal_device_set_property_bool (halctx, device_udi, "battery.present", TRUE, &err); LIBHAL_FREE_DBUS_ERROR (&err); if (!libhal_device_property_exists (halctx, device_udi, "battery.is_rechargeable", &err)) { LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_bool (halctx, device_udi, "battery.is_rechargeable", FALSE, &err); } LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_int (halctx, device_udi, "battery.charge_level.design", 7, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_int (halctx, device_udi, "battery.charge_level.last_full", 7, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_string (halctx, device_udi, "info.category", "battery", &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_string (halctx, device_udi, "battery.command_interface", "csr", &err); /* monitor change */ libhal_ctx_set_device_property_modified (halctx, property_modified); /* Initial fillup */ dev_props = property_cache_item_get (device_udi); HAL_ERROR (("** Initial fillup done")); /* init usb */ usb_init (); /* do coldplug */ check_all_batteries (NULL); /* only add capability when initial charge_level key has been set */ LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_add_capability (halctx, device_udi, "battery", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (!libhal_device_addon_is_ready (halctx, device_udi, &err)) { retval = -4; goto out; } hal_set_proc_title ("hald-addon-usb-csr: listening on '%s'", libhal_device_get_property_string(halctx, device_udi, "info.product", &err)); main_loop = g_main_loop_new (NULL, FALSE); #ifdef HAVE_GLIB_2_14 g_timeout_add_seconds (TIMEOUT, check_all_batteries, NULL); #else g_timeout_add (1000L * TIMEOUT, check_all_batteries, NULL); #endif g_main_loop_run (main_loop); return 0; out: HAL_DEBUG (("An error occured, exiting cleanly")); LIBHAL_FREE_DBUS_ERROR (&err); if (halctx != NULL) { libhal_ctx_shutdown (halctx, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_ctx_free (halctx); } return retval; }