seed-example.js revision ad29a9f14fa8b1932c0e418bfcf1c10ce6a35a33
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#!/usr/bin/env seed
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering// seed example
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringconst GLib = imports.gi.GLib;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringconst GUdev = imports.gi.GUdev;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringfunction print_device (device) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" subsystem: " + device.get_subsystem ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" devtype: " + device.get_devtype ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" name: " + device.get_name ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" number: " + device.get_number ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" sysfs_path: " + device.get_sysfs_path ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" driver: " + device.get_driver ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" action: " + device.get_action ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" seqnum: " + device.get_seqnum ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" device type: " + device.get_device_type ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" device number: " + device.get_device_number ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" device file: " + device.get_device_file ());
1b00a255225bd65bcc3f6257bd4b1e2089f231b4Tollef Fog Heen print (" device file symlinks: " + device.get_device_file_symlinks ());
e99e38bbdcca3fe5956823bdb3d38544ccf93221Lennart Poettering print (" foo: " + device.get_sysfs_attr_as_strv ("stat"));
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering var keys = device.get_property_keys ();
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering for (var n = 0; n < keys.length; n++) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print (" " + keys[n] + "=" + device.get_property (keys[n]));
e99e38bbdcca3fe5956823bdb3d38544ccf93221Lennart Poettering }
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering}
1b322bdb2a147e999c3861cba8a6b3ac0f3b712aTollef Fog Heen
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringfunction on_uevent (client, action, device) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print ("action " + action + " on device " + device.get_sysfs_path());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print_device (device);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print ("");
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering}
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringvar client = new GUdev.Client ({subsystems: ["block", "usb/usb_interface"]});
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringclient.signal.connect ("uevent", on_uevent);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringvar block_devices = client.query_by_subsystem ("block");
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringfor (var n = 0; n < block_devices.length; n++) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print ("block device: " + block_devices[n].get_device_file ());
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering}
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poetteringvar d;
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringd = client.query_by_device_number (GUdev.DeviceType.BLOCK, 0x0810);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringif (d == null) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print ("query_by_device_number 0x810 -> null");
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering} else {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering print ("query_by_device_number 0x810 -> " + d.get_device_file ());
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering dd = d.get_parent_with_subsystem ("usb", null);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering print_device (dd);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering print ("--------------------------------------------------------------------------");
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering while (d != null) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering print_device (d);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering print ("");
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering d = d.get_parent ();
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering }
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering}
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringd = client.query_by_sysfs_path ("/sys/block/sda/sda1");
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringprint ("query_by_sysfs_path (\"/sys/block/sda1\") -> " + d.get_device_file ());
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringd = client.query_by_subsystem_and_name ("block", "sda2");
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringprint ("query_by_subsystem_and_name (\"block\", \"sda2\") -> " + d.get_device_file ());
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringd = client.query_by_device_file ("/dev/sda");
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringprint ("query_by_device_file (\"/dev/sda\") -> " + d.get_device_file ());
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringd = client.query_by_device_file ("/dev/block/8:0");
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringprint ("query_by_device_file (\"/dev/block/8:0\") -> " + d.get_device_file ());
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringvar mainloop = GLib.main_loop_new ();
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart PoetteringGLib.main_loop_run (mainloop);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering