Logo Search packages:      
Sourcecode: hal version File versions

char ** hal_get_all_devices ( LibHalContext ctx,
int *  num_devices 

Get all devices in the Global Device List (GDL).

ctx The context for the connection to hald
num_devices The number of devices will be stored here
An array of device identifiers terminated with NULL. It is the responsibility of the caller to free with hal_free_string_array(). If an error occurs NULL is returned.

Definition at line 738 of file libhal.c.

References LibHalContext_s::connection.

Referenced by dump_devices().

      int i;
      DBusError error;
      DBusMessage *message;
      DBusMessage *reply;
      DBusMessageIter iter;
      char **device_names;
      char **hal_device_names;

      *num_devices = 0;

      message = dbus_message_new_method_call ("org.freedesktop.Hal",
      if (message == NULL) {
            fprintf (stderr,
                   "%s %d : Couldn't allocate D-BUS message\n",
                   __FILE__, __LINE__);
            return NULL;

      dbus_error_init (&error);
      reply = dbus_connection_send_with_reply_and_block (ctx->connection,
                                             message, -1,
      if (dbus_error_is_set (&error)) {
            fprintf (stderr, "%s %d : %s raised\n\"%s\"\n\n", __FILE__,
                   __LINE__, error.name, error.message);
            dbus_message_unref (message);
            return NULL;

      if (reply == NULL) {
            dbus_message_unref (message);
            return NULL;

      /* now analyze reply */
      dbus_message_iter_init (reply, &iter);
      if (!dbus_message_iter_get_string_array (&iter,
                                     num_devices)) {
            fprintf (stderr, "%s %d : wrong reply from hald\n",
                   __FILE__, __LINE__);
            return NULL;

      dbus_message_unref (reply);
      dbus_message_unref (message);

      /* Have to convert from dbus string array to hal string array 
       * since we can't poke at the dbus string array for the reason
       * that d-bus use their own memory allocation scheme
      hal_device_names = malloc (sizeof (char *) * ((*num_devices) + 1));
      if (hal_device_names == NULL)
            return NULL;
      /** @todo Handle OOM better */

      for (i = 0; i < (*num_devices); i++) {
            hal_device_names[i] = strdup (device_names[i]);
            if (hal_device_names[i] == NULL) {
                  fprintf (stderr,
                         "%s %d : error allocating memory\n",
                         __FILE__, __LINE__);
                  /** @todo FIXME cleanup */
                  return NULL;
      hal_device_names[i] = NULL;

      dbus_free_string_array (device_names);

      return hal_device_names;

Generated by  Doxygen 1.6.0   Back to index