(Java) How to get Hal DeviceAdded signal?
Berend Dekens
cyberwizzard at gmail.com
Wed Apr 23 12:16:39 PDT 2008
Hello,
I'm trying to figure out how to get notifications about new devices (I
want to auto-mount block devices and scan them for media files to be
exact). I am struggling with the DBus API and the HAL API. The docs
make it seem so easy and the first part (listing UDI's) went smoothly.
However, now I try to get the signals when a device is added but
whatever I try, nothing happens when I insert my USB drive (even
though KDE shows a popup, so at least dbus itself is working fine).
I currently have 2 classes, the org.freedesktop.Hal.Manager interface
and the class tying it together (the classes are below for
readability). I tried adding the Signal in multiple ways but I get no
errors and no events. Except when I use the public <T extends
DBusSignal> void addSigHandler(Class<T> type, String source,
DBusInterface object, DBusSigHandler<T> handler) method in the
DBusConnection class: I have no clue what source should and everything
I insert in there either results in a invalid bus name or well known
name error.
I googled all night long but I can't find further examples or docs to
turn me in the right direction. Can someone help me?
Regards,
Berend Dekens
-------------------------------- org/freedesktop/Hal/Manager.java
----------------------------------
package org.freedesktop.Hal;
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
/**
* Java interface for the org.freedesktop.Hal.Manager interface which
handles most of the system generated
* events. Based upon the Hal 0.5.10 specification at
http://people.freedesktop.org/~david/hal-spec/hal-spec.html#interface-manager
* @author cyberwizzard
*
*/
public interface Manager extends DBusInterface {
/**
* Get all UDI's in the database.
* @return Objref[] An array of UDI's - Strings for all I know
*/
public String[] GetAllDevices();
/**
* Determines if a given object exists.
* @return True if the device exists.
*/
public boolean DeviceExists();
/**
* Find devices for which the given string property assumes the given value.
* @param key
* @param value
* @return Objref[] An array of UDI's - Strings for all I know
*/
public String[] FindDeviceStringMatch(String key, String value);
/**
* Finds devices of the given capability.
* @param capability
* @return Objref[] An array of UDI's - Strings for all I know
*/
public String[] FindDeviceByCapability(String capability);
/**
* Creates a new device object in the temporary device list (TDL)
and return the UDI. Caller must be uid 0.
* @return Objref
* @throws DBusExecutionException PermissionDenied when you lack
the right permissions
*/
public String NewDevice() throws DBusExecutionException;
/**
* Removes a device object that was created in the TDL. Caller
must be uid 0.
* @param tmp_udi Objref tmp_udi
* @throws DBusExecutionException NoSuchDevice, PermissionDenied
*/
public void Remove(String tmp_udi) throws DBusExecutionException;
/**
* Moves a device from the temporary device list (TDL) to the
global device list (GDL). Caller must be uid 0.
* @param tmp_udi Objref tmp_udi
* @param udi Objref udi
* @throws DBusExecutionException NoSuchDevice, PermissionDenied
*/
public void CommitToGdl(String tmp_udi, String udi) throws
DBusExecutionException;
/**
* Acquires a global lock on an interface. See Chapter 4, Locking
for details
* @param interface_name String interface_name
* @param exclusive Bool exclusive
* @throws DBusExecutionException Device.InterfaceAlreadyLocked
*/
public void AcquireGlobalInterfaceLock(String interface_name,
boolean exclusive) throws DBusExecutionException;
/**
* Releases a global lock on an interface. See Chapter 4, Locking
for details.
* @param interface_name String interface_name
* @throws DBusExecutionException Device.InterfaceNotLocked
*/
public void ReleaseGlobalInterfaceLock(String interface_name)
throws DBusExecutionException;
/**
* DBus Signal DeviceAdded(Objref obj) is send by
org.freedesktop.Hal.Manager
* A device was added to the global device list (GDL).
* Based on the HAL 0.5.10 specs.
* @author cyberwizzard
*/
public class DeviceAdded extends DBusSignal {
public final String obj;
public DeviceAdded(String obj) throws DBusException {
super(obj);
this.obj = obj;
}
}
}
----------------------------------------- DbusMonitor.java
---------------------------------------
package org.omegacar.monitor;
import org.freedesktop.Hal.Manager;
import org.freedesktop.dbus.*;
import org.freedesktop.dbus.exceptions.DBusException;
public class DbusMonitor implements DBusSigHandler {
public void handle(DBusSignal s) {
System.out.println("SIGNAL!!");
if (s instanceof Manager.DeviceAdded)
System.out.println("Got a device connect for " +
((Manager.DeviceAdded) s).obj);
}
/**
* Make sure we cannot be instanced, the DBus stuff will create
multiple instances for
* callbacks and their handlers.
*/
protected DbusMonitor() {}
public static void createMonitor() {
System.out.println("Creating DBus Connection");
DBusConnection conn = null;
try {
conn =
DBusConnection.getConnection(DBusConnection.SYSTEM);
} catch (DBusException DBe) {
System.out.println("Could not connect to bus: " + DBe);
System.exit(1);
}
Manager HAL = null;
try {
HAL =
(Manager)conn.getRemoteObject("org.freedesktop.Hal","/org/freedesktop/Hal/Manager",Manager.class);
System.out.println("Printing UDI's");
String UDI[] = HAL.GetAllDevices();
for(int i = 0; i < UDI.length; i++) {
System.out.println("UDI: " + UDI[i]);
}
conn.addSigHandler(Manager.DeviceAdded.class,
new DbusMonitor());
//conn.addSigHandler(Manager.DeviceAdded.class, HAL, new
DbusMonitor());
//conn.addSigHandler(DbusMonitor.DeviceAdded.class,
"org.freedesktop.Hal.Manager", HAL, new DbusMonitor());
} catch (DBusException DBe) {
System.out.println("Could not set connect to
HAL: " + DBe);
conn.disconnect();
System.exit(1);
}
System.out.println("DBus hooking complete");
}
}
More information about the dbus
mailing list