/* taken from libipoddevice proper */ static ItdbBackend *hal_backend_new (void) { LibHalContext *hal_context; HalBackend *backend; const char *udi; DBusError error; DBusConnection *dbus_connection; udi = g_getenv ("UDI"); if (udi == NULL) { return NULL; } hal_context = libhal_ctx_new(); if(hal_context == NULL) { return NULL; } dbus_error_init(&error); dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); if (dbus_error_is_set(&error)) { dbus_error_free(&error); libhal_ctx_free(hal_context); return NULL; } libhal_ctx_set_dbus_connection(hal_context, dbus_connection); if(!libhal_ctx_init(hal_context, &error)) { if (dbus_error_is_set(&error)) { dbus_error_free(&error); } libhal_ctx_free(hal_context); return NULL; } backend = g_new0 (HalBackend, 1); backend->ctx = hal_context; backend->udi = g_strdup (udi); backend->parent.destroy = hal_backend_destroy; backend->parent.set_version = hal_backend_set_version; backend->parent.set_is_unknown = hal_backend_set_is_unknown; backend->parent.set_icon_name = hal_backend_set_icon_name; backend->parent.set_firewire_id = hal_backend_set_firewire_id; backend->parent.set_serial_number = hal_backend_set_serial_number; backend->parent.set_firmware_version = hal_backend_set_firmware_version; backend->parent.set_model_name = hal_backend_set_model_name; backend->parent.set_generation = hal_backend_set_generation; backend->parent.set_color = hal_backend_set_color; backend->parent.set_factory_id = hal_backend_set_factory_id; backend->parent.set_production_year = hal_backend_set_production_year; backend->parent.set_production_week = hal_backend_set_production_week; backend->parent.set_production_index = hal_backend_set_production_index; backend->parent.set_control_path = hal_backend_set_control_path; backend->parent.set_name = hal_backend_set_name; backend->parent.set_artwork_formats = hal_backend_set_artwork_formats; return (ItdbBackend *)backend; }
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 char * uuid_get_from_hal(void) { LibHalContext *ctx; DBusError error; DBusConnection *con; dbus_error_init(&error); if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ) { goto bailout_nobus; } ctx = libhal_ctx_new(); libhal_ctx_set_dbus_connection(ctx, con); if (!libhal_ctx_init(ctx, &error)) { goto bailout; } if (!libhal_device_property_exists(ctx, UUID_PATH, UUID_PROPERTY, &error)) { goto bailout; } char *uuid = libhal_device_get_property_string(ctx, UUID_PATH, UUID_PROPERTY, &error); if (looks_like_a_uuid (uuid)) { return uuid; } bailout: { DBusError ctxerror; dbus_error_init(&ctxerror); if (!(libhal_ctx_shutdown(ctx, &ctxerror))) { dbus_error_free(&ctxerror); } } libhal_ctx_free(ctx); //dbus_connection_unref(con); bailout_nobus: if (dbus_error_is_set(&error)) { /*printf("Error %s\n", error.name);*/ dbus_error_free(&error); } return NULL; }
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; }
/* FIXME: We should really change the name of this func*/ LibHalContext* libhal_context_alloc(void) { LibHalContext *hal_ctx = NULL; DBusConnection *system_bus = NULL; DBusError error; hal_ctx = libhal_ctx_new (); if (hal_ctx == NULL) { g_warning ("Failed to get libhal context"); goto error; } dbus_error_init (&error); system_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (dbus_error_is_set (&error)) { g_warning ("Cannot connect to system bus: %s : %s", error.name, error.message); dbus_error_free (&error); goto error; } libhal_ctx_set_dbus_connection (hal_ctx, system_bus); if (!libhal_ctx_init (hal_ctx, &error)) { g_warning ("Failed to initialize libhal context: %s : %s", error.name, error.message); dbus_error_free (&error); goto error; } return hal_ctx; error: if (hal_ctx != NULL) libhal_ctx_free (hal_ctx); return NULL; }
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; }
int connect_hal(void) { DBusError error; dbus_error_init(&error); dbus_ctx = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); if (dbus_ctx == NULL) { fprintf(stderr, "error: dbus_bus_get: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR(&error); return 0; } hal_ctx = libhal_ctx_new(); if (hal_ctx == NULL) { fprintf(stderr, "error: libhal_ctx_new\n"); LIBHAL_FREE_DBUS_ERROR(&error); return 0; } if (!libhal_ctx_set_dbus_connection(hal_ctx, dbus_ctx)) { fprintf(stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR(&error); return 0; } if (!libhal_ctx_init(hal_ctx, &error)) { if (dbus_error_is_set(&error)) { fprintf(stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR(&error); } fprintf(stderr, "Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready.\n"); return 0; } return 1; }
// 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; }
gboolean check_libhal (const char *server_addr) { pid_t child_pid; child_pid = fork (); if (child_pid == -1) { printf ("Cannot fork\n"); exit (1); } else if (child_pid == 0) { DBusError error; DBusConnection *conn; LibHalContext *ctx; dbus_bool_t passed; printf ("server address='%s'\n", server_addr); dbus_error_init (&error); if ((conn = dbus_connection_open (server_addr, &error)) == NULL) { printf ("Error connecting to server: %s\n", error.message); goto fail; } dbus_connection_setup_with_g_main (conn, NULL); if ((ctx = libhal_ctx_new ()) == NULL) { printf ("Error getting libhal context\n"); goto fail; } libhal_ctx_set_dbus_connection (ctx, conn); libhal_ctx_init (ctx, &error); if (dbus_error_is_set (&error)) { printf ("FAILED98: %s\n", error.message); goto fail; } printf ("SUCCESS98\n"); libhal_device_print (ctx, "/org/freedesktop/Hal/devices/testobj1", &error); if (dbus_error_is_set (&error)) { printf ("FAILED99: %s\n", error.message); goto fail; } printf ("SUCCESS99\n"); passed = FALSE; { char *val; val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string", &error); if (val == NULL || strcmp (val, "fooooobar22") != 0 || dbus_error_is_set (&error)) { libhal_free_string (val); printf ("FAILED100\n"); goto fail; } printf ("SUCCESS100\n"); libhal_free_string (val); } { char *val; val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string2", &error); if (val == NULL || strcmp (val, "fooøةמ") != 0 || dbus_error_is_set (&error)) { libhal_free_string (val); printf ("FAILED101: %s\n", error.message); goto fail; } libhal_free_string (val); printf ("SUCCESS101\n"); } { dbus_bool_t b; b = libhal_device_get_property_bool ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.bool", &error); if (!b || dbus_error_is_set (&error)) { printf ("FAILED102: %s, %i\n", error.message, b); goto fail; } printf ("SUCCESS102\n"); } { double val; double expected_val = 0.53434343; val = libhal_device_get_property_double (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.double", &error); if ( memcmp (&val, &expected_val, sizeof (double)) != 0 || dbus_error_is_set (&error)) { printf ("FAILED103\n"); goto fail; } printf ("SUCCESS103\n"); } if (libhal_device_get_property_uint64 ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.uint64", &error) != ((((dbus_uint64_t)1)<<35) + 5) || dbus_error_is_set (&error)) { printf ("FAILED104: %s\n", error.message); goto fail; } printf ("SUCCESS104\n"); { char **val; val = libhal_device_get_property_strlist (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.strlist", &error); if (val == NULL || dbus_error_is_set (&error)) { libhal_free_string_array (val); printf ("FAILED105: %s\n", error.message); goto fail; } printf ("SUCCESS105\n"); if (libhal_string_array_length (val) != 2) { libhal_free_string_array (val); printf ("FAILED106\n"); goto fail; } printf ("SUCCESS106\n"); if (strcmp (val[0], "foostrlist2") != 0 || strcmp (val[1], "foostrlist3") != 0) { libhal_free_string_array (val); printf ("FAILED107\n"); goto fail; } printf ("SUCCESS107\n"); libhal_free_string_array (val); } if (libhal_device_get_property_int ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.int", &error) != 42 || dbus_error_is_set (&error)) { printf ("FAILED108\n"); goto fail; } printf ("SUCCESS108\n"); /* tests for libhal_psi */ { int type; char *key; LibHalPropertySet *pset; LibHalPropertySetIterator it; unsigned int psi_num_passed; unsigned int psi_num_elems; if ((pset = libhal_device_get_all_properties (ctx, "/org/freedesktop/Hal/devices/testobj1", &error)) == NULL) return FALSE; printf ("SUCCESS110\n"); psi_num_passed = 0; psi_num_elems = libhal_property_set_get_num_elems (pset); for (libhal_psi_init (&it, pset); libhal_psi_has_more (&it); libhal_psi_next (&it)) { type = libhal_psi_get_type (&it); key = libhal_psi_get_key (&it); switch (type) { case LIBHAL_PROPERTY_TYPE_STRING: if (strcmp (key, "info.udi") == 0) { if (strcmp (libhal_psi_get_string (&it), "/org/freedesktop/Hal/devices/testobj1") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } else if (strcmp (key, "test.string") == 0) { if (strcmp (libhal_psi_get_string (&it), "fooooobar22") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } else if (strcmp (key, "test.string2") == 0) { if (strcmp (libhal_psi_get_string (&it), "fooøةמ") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } break; case LIBHAL_PROPERTY_TYPE_INT32: if (strcmp (key, "test.int") == 0) { if (libhal_psi_get_int (&it) == 42) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_UINT64: if (strcmp (key, "test.uint64") == 0) { if (libhal_psi_get_uint64 (&it) == ((((dbus_uint64_t)1)<<35) + 5)) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_BOOLEAN: if (strcmp (key, "test.bool") == 0) { if (libhal_psi_get_bool (&it) == TRUE) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_DOUBLE: if (strcmp (key, "test.double") == 0) { double val = 0.53434343; double val2; val2 = libhal_psi_get_double (&it); if (memcmp (&val, &val2, sizeof (double)) == 0) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_STRLIST: if (strcmp (key, "test.strlist") == 0) { char **val; val = libhal_psi_get_strlist (&it); if (libhal_string_array_length (val) == 2 && strcmp (val[0], "foostrlist2") == 0 && strcmp (val[1], "foostrlist3") == 0 && val[2] == NULL) psi_num_passed++; else printf ("fail on %s\n", key); } break; default: printf (" *** unknown type for key %s\n", key); break; } } libhal_free_property_set (pset); if (psi_num_passed != psi_num_elems) { printf ("FAILED111\n"); goto fail; } printf ("SUCCESS111\n"); } /* end libhal_test_psi */ printf ("Passed all libhal tests\n"); passed = TRUE; fail: if (dbus_error_is_set (&error)) dbus_error_free (&error); send_tests_done (conn, passed); exit (1); } else { printf ("child pid=%d\n", child_pid); } return TRUE; }
static void get_video_devices (ofGstCamData & cam_data) { int i, fd, ok; int num_udis = 0; char **udis; DBusError error; LibHalContext *hal_ctx; cam_data.num_webcam_devices = 0; g_print ("Probing devices with HAL...\n"); dbus_error_init (&error); hal_ctx = libhal_ctx_new (); if (hal_ctx == NULL) { g_warning ("Could not create libhal context"); dbus_error_free (&error); goto fallback; } if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { g_warning ("libhal_ctx_set_dbus_connection: %s: %s", error.name, error.message); dbus_error_free (&error); goto fallback; } if (!libhal_ctx_init (hal_ctx, &error)) { if (dbus_error_is_set (&error)) { g_warning ("libhal_ctx_init: %s: %s", error.name, error.message); dbus_error_free (&error); } g_warning ("Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready"); goto fallback; } udis = libhal_find_device_by_capability (hal_ctx, "video4linux", &num_udis, &error); if (dbus_error_is_set (&error)) { g_warning ("libhal_find_device_by_capability: %s: %s", error.name, error.message); dbus_error_free (&error); goto fallback; } /* Initialize webcam structures */ cam_data.webcam_devices = new ofGstDevice[num_udis]; for (i = 0; i < num_udis; i++) { char *device; char *parent_udi = NULL; char *subsystem = NULL; char *gstreamer_src, *product_name; struct v4l2_capability v2cap; struct video_capability v1cap; gint vendor_id = 0; gint product_id = 0; gchar *property_name = NULL; parent_udi = libhal_device_get_property_string (hal_ctx, udis[i], "info.parent", &error); if (dbus_error_is_set (&error)) { g_warning ("error getting parent for %s: %s: %s", udis[i], error.name, error.message); dbus_error_free (&error); } if (parent_udi != NULL) { subsystem = libhal_device_get_property_string (hal_ctx, parent_udi, "info.subsystem", NULL); if (subsystem == NULL) continue; property_name = g_strjoin (".", subsystem, "vendor_id", NULL); vendor_id = libhal_device_get_property_int (hal_ctx, parent_udi, property_name , &error); if (dbus_error_is_set (&error)) { g_warning ("error getting vendor id: %s: %s", error.name, error.message); dbus_error_free (&error); } g_free (property_name); property_name = g_strjoin (".", subsystem, "product_id", NULL); product_id = libhal_device_get_property_int (hal_ctx, parent_udi, property_name, &error); if (dbus_error_is_set (&error)) { g_warning ("error getting product id: %s: %s", error.name, error.message); dbus_error_free (&error); } g_free (property_name); libhal_free_string (subsystem); libhal_free_string (parent_udi); } g_print ("Found device %04x:%04x, getting capabilities...\n", vendor_id, product_id); device = libhal_device_get_property_string (hal_ctx, udis[i], "video4linux.device", &error); if (dbus_error_is_set (&error)) { g_warning ("error getting V4L device for %s: %s: %s", udis[i], error.name, error.message); dbus_error_free (&error); continue; } /* vbi devices support capture capability too, but cannot be used, * so detect them by device name */ if (strstr (device, "vbi")) { g_print ("Skipping vbi device: %s\n", device); libhal_free_string (device); continue; } if ((fd = open (device, O_RDONLY | O_NONBLOCK)) < 0) { g_warning ("Failed to open %s: %s", device, strerror (errno)); libhal_free_string (device); continue; } ok = ioctl (fd, VIDIOC_QUERYCAP, &v2cap); if (ok < 0) { ok = ioctl (fd, VIDIOCGCAP, &v1cap); if (ok < 0) { g_warning ("Error while probing v4l capabilities for %s: %s", device, strerror (errno)); libhal_free_string (device); close (fd); continue; } g_print ("Detected v4l device: %s\n", v1cap.name); g_print ("Device type: %d\n", v1cap.type); gstreamer_src = "v4lsrc"; product_name = v1cap.name; } else { guint cap = v2cap.capabilities; g_print ("Detected v4l2 device: %s\n", v2cap.card); g_print ("Driver: %s, version: %d\n", v2cap.driver, v2cap.version); /* g_print ("Bus info: %s\n", v2cap.bus_info); */ /* Doesn't seem anything useful */ g_print ("Capabilities: 0x%08X\n", v2cap.capabilities); if (!(cap & V4L2_CAP_VIDEO_CAPTURE)) { g_print ("Device %s seems to not have the capture capability, (radio tuner?)\n" "Removing it from device list.\n", device); libhal_free_string (device); close (fd); continue; } gstreamer_src = "v4l2src"; product_name = (char *) v2cap.card; } g_print ("\n"); cam_data.webcam_devices[cam_data.num_webcam_devices].hal_udi = g_strdup (udis[i]); cam_data.webcam_devices[cam_data.num_webcam_devices].video_device = g_strdup (device); cam_data.webcam_devices[cam_data.num_webcam_devices].gstreamer_src = g_strdup (gstreamer_src); cam_data.webcam_devices[cam_data.num_webcam_devices].product_name = g_strdup (product_name); cam_data.webcam_devices[cam_data.num_webcam_devices].num_video_formats = 0; cam_data.webcam_devices[cam_data.num_webcam_devices].supported_resolutions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); cam_data.num_webcam_devices++; libhal_free_string (device); close (fd); } libhal_free_string_array (udis); if (cam_data.num_webcam_devices == 0) { /* Create a fake device so that resolution changing stil works even if the * computer doesn't have a webcam. */ fallback: if (num_udis == 0) { cam_data.webcam_devices = new ofGstDevice; } cam_data.webcam_devices[0].num_video_formats = 0; cam_data.webcam_devices[0].hal_udi = g_strdup ("oF_fake_videodevice"); } cam_data.bInited=true; }
int main(int argc, char **argv) { DBusError error; DBusConnection *conn; LibHalContext *hal_ctx; int err; int c; int all = 0; int list = 0; int dev = 0; char *dev_name; if (argc < 2) { return (0); } while ((c = getopt(argc, argv, "ald:")) != EOF) { switch (c) { case 'a': all = 1; break; case 'l': list = 1; break; case 'd': dev = 1; dev_name = optarg; break; default: exit(1); } } dbus_error_init(&error); if (!(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { fprintf(stderr, "error: dbus_bus_get: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR(&error); return (2); } if (!(hal_ctx = libhal_ctx_new())) { return (3); } if (!libhal_ctx_set_dbus_connection(hal_ctx, conn)) { return (4); } if (!libhal_ctx_init(hal_ctx, &error)) { if (dbus_error_is_set(&error)) { fprintf(stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR(&error); } fprintf(stderr, "Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready.\n"); return (5); } err = 0; if (list) { err = list_battery_devices(hal_ctx); } if (all) { err = dump_all_devices(hal_ctx); } if (dev) { err = dump_device(hal_ctx, dev_name); } libhal_ctx_shutdown(hal_ctx, &error); libhal_ctx_free(hal_ctx); dbus_connection_unref(conn); dbus_error_free(&error); return (err); }
int main (int argc, char *argv[]) { DBusConnection *dbconn; DBusError dberr; LibHalContext *hal_ctx; LibHalPropertyType property_type; char *property_key; dbus_bool_t bool_value; int int_value; char **udis; int num_udis; int i; dbus_error_init (&dberr); dbconn = dbus_bus_get (DBUS_BUS_SYSTEM, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "Can't get D-Bus system bus!"); return EXIT_FAILURE; } hal_ctx = libhal_ctx_new (); if (hal_ctx == NULL) { fprintf (stderr, "Can't create a LibHalContext!"); return EXIT_FAILURE; } /* Associate HAL with the D-Bus connection we established */ libhal_ctx_set_dbus_connection (hal_ctx, dbconn); dbus_error_init (&dberr); libhal_ctx_init (hal_ctx, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "libhal_ctx_init() failed: '%s'. Is hald running?", dberr.message); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Looking for optical drives: storage.cdrom is the capability */ udis = libhal_find_device_by_capability (hal_ctx, "storage.cdrom", &num_udis, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "libhal_find_device_by_capability error: '%s'\n", dberr.message); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } printf ("Found %d Optical Device(s)\n", num_udis); for (i = 0; i < num_udis; i++) { /* Ensure our properties are the expected type */ property_type = libhal_device_get_property_type (hal_ctx, udis[i], "storage.cdrom.dvd", &dberr); if (dbus_error_is_set (&dberr) || property_type != LIBHAL_PROPERTY_TYPE_BOOLEAN) { fprintf (stderr, "error checking storage.cdrom.dvd type"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } property_type = libhal_device_get_property_type (hal_ctx, udis[i], "storage.cdrom.read_speed", &dberr); if (dbus_error_is_set (&dberr) || property_type != LIBHAL_PROPERTY_TYPE_INT32) { fprintf (stderr, "error checking storage.cdrom.read_speed type"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Okay, now simply get property values */ bool_value = libhal_device_get_property_bool (hal_ctx, udis[i], "storage.cdrom.dvd", &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "error getting storage.cdrom.dvd"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } int_value = libhal_device_get_property_int (hal_ctx, udis[i], "storage.cdrom.read_speed", &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "error getting storage.cdrom.dvd"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Display the info we just got */ printf ("Device %s has a maximum read spead of %d kb/s and %s read DVDs.\n", udis[i], int_value, bool_value ? "can" : "cannot"); } return EXIT_SUCCESS; }
std::map<int, std::pair<std::string, std::string> > SerialPortEnumerator::getPorts() { std::map<int, std::pair<std::string, std::string> > ports; #ifdef MACOSX // use IOKit to enumerates devices // get a matching dictionary to specify which IOService class we're interested in CFMutableDictionaryRef classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (classesToMatch == NULL) throw DashelException(DashelException::EnumerationError, 0, "IOServiceMatching returned a NULL dictionary"); // specify all types of serial devices CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes)); // get an iterator to serial port services io_iterator_t matchingServices; kern_return_t kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &matchingServices); if (KERN_SUCCESS != kernResult) throw DashelException(DashelException::EnumerationError, kernResult, "IOServiceGetMatchingServices failed"); // iterate over services io_object_t modemService; int index = 0; while((modemService = IOIteratorNext(matchingServices))) { // get path for device CFTypeRef bsdPathAsCFString = IORegistryEntryCreateCFProperty(modemService, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0); if (bsdPathAsCFString) { std::string path; char cStr[255]; std::string name; bool res = CFStringGetCString((CFStringRef) bsdPathAsCFString, cStr, 255, kCFStringEncodingUTF8); if(res) path = cStr; else throw DashelException(DashelException::EnumerationError, 0, "CFStringGetCString failed"); CFRelease(bsdPathAsCFString); CFTypeRef fn = IORegistryEntrySearchCFProperty(modemService, kIOServicePlane, CFSTR("USB Product Name"), kCFAllocatorDefault, kIORegistryIterateRecursively | kIORegistryIterateParents); if(fn) { res = CFStringGetCString((CFStringRef) fn, cStr, 255, kCFStringEncodingUTF8); if(res) name = cStr; else throw DashelException(DashelException::EnumerationError, 0, "CFStringGetString failed"); CFRelease(fn); } else name = "Serial Port"; name = name + " (" + path + ")"; ports[index++] = std::make_pair<std::string, std::string>(path, name); } else throw DashelException(DashelException::EnumerationError, 0, "IORegistryEntryCreateCFProperty returned a NULL path"); // release service IOObjectRelease(modemService); } IOObjectRelease(matchingServices); #elif defined(USE_LIBUDEV) struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; struct udev_device *dev; int index = 0; udev = udev_new(); if(!udev) throw DashelException(DashelException::EnumerationError, 0, "Cannot create udev context"); enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "tty"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); udev_list_entry_foreach(dev_list_entry, devices) { const char *sysfs_path; struct udev_device *usb_dev; const char * path; struct stat st; unsigned int maj,min; /* Get sysfs path and create the udev device */ sysfs_path = udev_list_entry_get_name(dev_list_entry); dev = udev_device_new_from_syspath(udev, sysfs_path); // Some sanity check path = udev_device_get_devnode(dev); if(stat(path, &st)) throw DashelException(DashelException::EnumerationError, 0, "Cannot stat serial port"); if(!S_ISCHR(st.st_mode)) throw DashelException(DashelException::EnumerationError, 0, "Serial port is not character device"); // Get the major/minor number maj = major(st.st_rdev); min = minor(st.st_rdev); // Ignore all the non physical ports if(!(maj == 2 || (maj == 4 && min < 64) || maj == 3 || maj == 5)) { ostringstream oss; // Check if usb, if yes get the device name usb_dev = udev_device_get_parent_with_subsystem_devtype(dev,"usb","usb_device"); if(usb_dev) oss << udev_device_get_sysattr_value(usb_dev,"product"); else oss << "Serial Port"; oss << " (" << path << ")"; ports[index++] = std::make_pair<std::string, std::string>(path,oss.str()); } udev_device_unref(dev); } udev_enumerate_unref(enumerate); udev_unref(udev); #elif defined(USE_HAL) // use HAL to enumerates devices DBusConnection* dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, 0); if (!dbusConnection) throw DashelException(DashelException::EnumerationError, 0, "cannot connect to D-BUS."); LibHalContext* halContext = libhal_ctx_new(); if (!halContext) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot create context"); if (!libhal_ctx_set_dbus_connection(halContext, dbusConnection)) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot connect to D-BUS"); if (!libhal_ctx_init(halContext, 0)) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot init context"); int devicesCount; char** devices = libhal_find_device_by_capability(halContext, "serial", &devicesCount, 0); for (int i = 0; i < devicesCount; i++) { char* devFileName = libhal_device_get_property_string(halContext, devices[i], "serial.device", 0); char* info = libhal_device_get_property_string(halContext, devices[i], "info.product", 0); int port = libhal_device_get_property_int(halContext, devices[i], "serial.port", 0); ostringstream oss; oss << info << " " << port; ports[devicesCount - i] = std::make_pair<std::string, std::string>(devFileName, oss.str()); libhal_free_string(info); libhal_free_string(devFileName); } libhal_free_string_array(devices); libhal_ctx_shutdown(halContext, 0); libhal_ctx_free(halContext); #endif return ports; };
static gboolean volume_is_nokia770 (GVolume *volume) { LibHalContext *ctx; DBusConnection *conn; char *parent_udi, *udi; char *parent_name; gboolean result; DBusError error; gboolean inited = FALSE; result = FALSE; dbus_error_init (&error); conn = NULL; udi = NULL; parent_udi = NULL; parent_name = NULL; ctx = libhal_ctx_new (); if (ctx == NULL) { rb_debug ("cannot connect to HAL"); goto end; } conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (conn == NULL || dbus_error_is_set (&error)) goto end; libhal_ctx_set_dbus_connection (ctx, conn); if (!libhal_ctx_init (ctx, &error) || dbus_error_is_set (&error)) goto end; udi = rb_gvolume_get_udi (volume, ctx); if (udi == NULL) goto end; inited = TRUE; parent_udi = libhal_device_get_property_string (ctx, udi, "info.parent", &error); if (parent_udi == NULL || dbus_error_is_set (&error)) goto end; rb_debug ("Nokia detection: info.parent=%s", parent_udi); parent_name = libhal_device_get_property_string (ctx, parent_udi, "info.vendor", &error); rb_debug ("Nokia detection: info.vendor=%s", parent_name); if (parent_name == NULL || dbus_error_is_set (&error)) goto end; if (strcmp (parent_name, "Nokia") == 0) { g_free (parent_name); parent_name = libhal_device_get_property_string (ctx, parent_udi, "info.product", &error); rb_debug ("Nokia detection: info.product=%s", parent_name); if (parent_name == NULL || dbus_error_is_set (&error)) goto end; if (strcmp (parent_name, "770") == 0) { result = TRUE; } else if (strcmp (parent_name, "N800") == 0) { result = TRUE; } } end: g_free (udi); g_free (parent_name); g_free (parent_udi); if (dbus_error_is_set (&error)) { rb_debug ("Error: %s\n", error.message); dbus_error_free (&error); dbus_error_init (&error); } if (ctx) { if (inited) libhal_ctx_shutdown (ctx, &error); libhal_ctx_free (ctx); } dbus_error_free (&error); return result; }
/** Entry point * * @param argc Number of arguments given to program * @param argv Arguments given to program * @return Return code */ int main (int argc, char *argv[]) { char *udi = NULL; char *key = NULL; int type; dbus_bool_t is_hex = FALSE; dbus_bool_t is_verbose = FALSE; dbus_bool_t is_version = FALSE; char *str; DBusError error; if (argc <= 1) { usage (argc, argv); return 1; } while (1) { int c; int option_index = 0; const char *opt; static struct option long_options[] = { {"udi", 1, NULL, 0}, {"key", 1, NULL, 0}, {"hex", 0, NULL, 0}, {"verbose", 0, NULL, 0}, {"version", 0, NULL, 0}, {"help", 0, NULL, 0}, {NULL, 0, NULL, 0} }; c = getopt_long (argc, argv, "", long_options, &option_index); if (c == -1) break; switch (c) { case 0: opt = long_options[option_index].name; if (strcmp (opt, "help") == 0) { usage (argc, argv); return 0; } else if (strcmp (opt, "hex") == 0) { is_hex = TRUE; } else if (strcmp (opt, "verbose") == 0) { is_verbose = TRUE; } else if (strcmp (opt, "version") == 0) { is_version = TRUE; } else if (strcmp (opt, "key") == 0) { key = strdup (optarg); } else if (strcmp (opt, "udi") == 0) { udi = strdup (optarg); } break; default: usage (argc, argv); return 1; break; } } if (is_version) { printf ("hal-get-property " PACKAGE_VERSION "\n"); return 0; } if (udi == NULL || key == NULL) { usage (argc, argv); return 1; } dbus_error_init (&error); if ((hal_ctx = libhal_ctx_new ()) == NULL) { fprintf (stderr, "error: libhal_ctx_new\n"); return 1; } if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } if (!libhal_ctx_init (hal_ctx, &error)) { if (dbus_error_is_set(&error)) { fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); } fprintf (stderr, "Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready.\n"); return 1; } type = libhal_device_get_property_type (hal_ctx, udi, key, &error); if (type == LIBHAL_PROPERTY_TYPE_INVALID) { fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } /* emit the value to stdout */ switch (type) { case LIBHAL_PROPERTY_TYPE_STRING: str = libhal_device_get_property_string (hal_ctx, udi, key, &error); if (is_verbose) printf ("Type is string\n"); printf ("%s\n", str); libhal_free_string (str); break; case LIBHAL_PROPERTY_TYPE_INT32: if (is_verbose) printf ("Type is integer (shown in %s)\n", (is_hex ? "hexadecimal" : "decimal")); printf ((is_hex ? "%x\n" : "%d\n"), libhal_device_get_property_int (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_UINT64: if (is_verbose) printf ("Type is uint64 (shown in %s)\n", (is_hex ? "hexadecimal" : "decimal")); printf ((is_hex ? "%llx\n" : "%llu\n"), (long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_DOUBLE: if (is_verbose) printf ("Type is double\n"); printf ("%f\n", libhal_device_get_property_double (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_BOOLEAN: if (is_verbose) printf ("Type is boolean\n"); printf ("%s\n", libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false"); break; case LIBHAL_PROPERTY_TYPE_STRLIST: { unsigned int i; char **strlist; if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) { for (i = 0; strlist[i] != 0; i++) { printf ("%s", strlist[i]); if (strlist[i+1] != NULL) printf (" "); } } break; } default: printf ("Unknown type %d='%c'\n", type, type); return 1; break; } if (dbus_error_is_set (&error)) { fprintf (stderr, "error: %s: %s\n", error.name, error.message); dbus_error_free (&error); return 1; } return 0; }
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; }
int main(int argc, char **argv) { DBusError error; DBusConnection *conn; LibHalContext *hal_ctx; int i, err; opterr = 0; opt.list = 1; while ((i = getopt_long(argc, argv, "a:hr:", options, NULL)) != -1) { switch (i) { case 'a': opt.add = 1; opt.list = 0; opt.udi = optarg; break; case 'r': opt.remove = 1; opt.list = 0; opt.udi = optarg; break; case 'h': help(); return 0; default: help(); return 1; } } dbus_error_init(&error); if (!(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { fprintf(stderr, "error: dbus_bus_get: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 2; } /* fprintf(stderr, "connected to: %s\n", dbus_bus_get_unique_name(conn)); */ if (!(hal_ctx = libhal_ctx_new())) return 3; if (!libhal_ctx_set_dbus_connection(hal_ctx, conn)) return 4; if (!libhal_ctx_init(hal_ctx, &error)) { if (dbus_error_is_set(&error)) { fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); } fprintf (stderr, "Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready.\n"); return 5; } err = 0; if (opt.list) err = dump_devices(hal_ctx, argv[optind]); else if (opt.remove) err = remove_udi(hal_ctx, opt.udi); else if (opt.add) err = add_udi(hal_ctx, opt.udi); else err = 6; libhal_ctx_shutdown(hal_ctx, &error); libhal_ctx_free(hal_ctx); dbus_connection_unref(conn); dbus_error_free(&error); return err; }
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; }
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); } }
/* * gst_hal_get_string: * @udi: a #gchar corresponding to the UDI you want to get. * @device_type: a #GstHalDeviceType specifying the wanted device type. * * Get Hal UDI @udi's string value. * * Returns: a newly allocated #gchar string containing the appropriate pipeline * for UDI @udi, or NULL in the case of an error.. */ static gchar * gst_hal_get_string (const gchar * udi, GstHalDeviceType device_type) { DBusError error; LibHalContext *ctx; char *string = NULL; /* Don't query HAL for NULL UDIs. Passing NULL as UDI to HAL gives * an assertion failure in D-Bus when running with * DBUS_FATAL_WARNINGS=1. */ if (!udi) return NULL; dbus_error_init (&error); ctx = libhal_ctx_new (); /* Should only happen on OOM */ g_return_val_if_fail (ctx != NULL, NULL); if (!libhal_ctx_set_dbus_connection (ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { GST_DEBUG ("Unable to set DBus connection: %s: %s", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); goto ctx_free; } if (!libhal_ctx_init (ctx, &error)) { GST_DEBUG ("Unable to set init HAL context: %s: %s", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); goto ctx_free; } /* Now first check if UDI is an alsa device, then oss and then * check the childs of the given device. If there are alsa and oss * children the first alsa one is used. */ string = gst_hal_get_alsa_element (ctx, udi, device_type); if (!string) string = gst_hal_get_oss_element (ctx, udi, device_type); if (!string) { int num_childs; char **childs = NULL; /* now try if one of the direct subdevices supports ALSA or OSS */ childs = libhal_manager_find_device_string_match (ctx, "info.parent", udi, &num_childs, &error); if (dbus_error_is_set (&error)) { GST_DEBUG ("Unable to retrieve childs of %s: %s: %s", udi, error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); goto ctx_shutdown; } if (childs && num_childs > 0) { int i; char *alsa_string = NULL, *oss_string = NULL; for (i = 0; i < num_childs && !alsa_string; i++) { alsa_string = gst_hal_get_alsa_element (ctx, childs[i], device_type); if (!oss_string) oss_string = gst_hal_get_oss_element (ctx, childs[i], device_type); } if (alsa_string) { string = alsa_string; g_free (oss_string); } else if (oss_string) { string = oss_string; } } libhal_free_string_array (childs); } ctx_shutdown: if (!libhal_ctx_shutdown (ctx, &error)) { GST_DEBUG ("Closing connection to HAL failed: %s: %s", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); } ctx_free: libhal_ctx_free (ctx); if (string == NULL) { GST_WARNING ("Problem finding a HAL audio device for udi %s", udi); } else { GST_INFO ("Using %s", string); } return string; }
/** * main: * @argc: Number of arguments given to program * @argv: Arguments given to program * * Returns: Return code * * Main entry point */ int main (int argc, char *argv[]) { dbus_bool_t rc = 0; char *udi = NULL; char *key = NULL; char *str_value = NULL; dbus_int32_t int_value = 0; dbus_uint64_t uint64_value = 0; double double_value = 0.0f; dbus_bool_t bool_value = TRUE; dbus_bool_t remove = FALSE; dbus_bool_t is_version = FALSE; int type = PROP_INVALID; DBusError error; dbus_bool_t direct = FALSE; if (argc <= 1) { usage (argc, argv); return 1; } while (1) { int c; int option_index = 0; const char *opt; static struct option long_options[] = { {"udi", 1, NULL, 0}, {"key", 1, NULL, 0}, {"int", 1, NULL, 0}, {"uint64", 1, NULL, 0}, {"string", 1, NULL, 0}, {"double", 1, NULL, 0}, {"bool", 1, NULL, 0}, {"strlist-pre", 1, NULL, 0}, {"strlist-post", 1, NULL, 0}, {"strlist-rem", 1, NULL, 0}, {"direct", 0, NULL, 0}, {"remove", 0, NULL, 0}, {"version", 0, NULL, 0}, {"help", 0, NULL, 0}, {NULL, 0, NULL, 0} }; c = getopt_long (argc, argv, "", long_options, &option_index); if (c == -1) break; switch (c) { case 0: opt = long_options[option_index].name; if (strcmp (opt, "help") == 0) { usage (argc, argv); return 0; } else if (strcmp (opt, "key") == 0) { key = strdup (optarg); } else if (strcmp (opt, "string") == 0) { str_value = strdup (optarg); type = PROP_STRING; } else if (strcmp (opt, "int") == 0) { int_value = strtol (optarg, NULL, 0); type = PROP_INT; } else if (strcmp (opt, "uint64") == 0) { uint64_value = strtoull (optarg, NULL, 0); type = PROP_UINT64; } else if (strcmp (opt, "double") == 0) { double_value = (double) atof (optarg); type = PROP_DOUBLE; } else if (strcmp (opt, "bool") == 0) { if (strcmp (optarg, "true") == 0) bool_value = TRUE; else if (strcmp (optarg, "false") == 0) bool_value = FALSE; else { usage (argc, argv); return 1; } type = PROP_BOOL; } else if (strcmp (opt, "strlist-pre") == 0) { str_value = strdup (optarg); type = PROP_STRLIST_PRE; } else if (strcmp (opt, "strlist-post") == 0) { str_value = strdup (optarg); type = PROP_STRLIST_POST; } else if (strcmp (opt, "strlist-rem") == 0) { str_value = strdup (optarg); type = PROP_STRLIST_REM; } else if (strcmp (opt, "remove") == 0) { remove = TRUE; } else if (strcmp (opt, "direct") == 0) { direct = TRUE; } else if (strcmp (opt, "udi") == 0) { udi = strdup (optarg); } else if (strcmp (opt, "version") == 0) { is_version = TRUE; } break; default: usage (argc, argv); return 1; break; } } if (is_version) { printf ("hal-set-property " PACKAGE_VERSION "\n"); return 0; } /* must have at least one, but not neither or both */ if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) { usage (argc, argv); return 1; } fprintf (stderr, "\n"); dbus_error_init (&error); if (direct) { if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) { fprintf (stderr, "error: libhal_ctx_init_direct\n"); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } } else { if ((hal_ctx = libhal_ctx_new ()) == NULL) { fprintf (stderr, "error: libhal_ctx_new\n"); return 1; } if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } if (!libhal_ctx_init (hal_ctx, &error)) { if (dbus_error_is_set(&error)) { fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); dbus_error_free (&error); } fprintf (stderr, "Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready.\n"); return 1; } } if (remove) { rc = libhal_device_remove_property (hal_ctx, udi, key, &error); if (!rc) { fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } } else { switch (type) { case PROP_STRING: rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error); break; case PROP_INT: rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error); break; case PROP_UINT64: rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error); break; case PROP_DOUBLE: rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error); break; case PROP_BOOL: rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error); break; case PROP_STRLIST_PRE: rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error); break; case PROP_STRLIST_POST: rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error); break; case PROP_STRLIST_REM: rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error); break; } if (!rc) { fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message); dbus_error_free (&error); return 1; } } return rc ? 0 : 1; }
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; }
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); } }