This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

Vala USB Sample

Listing USB devices

This sample shows usage of LibUSB to display currently connected USB devices.

Requires Vala 0.14.0 & libusb 1.0 ("libusb-1.0" on Debian/Ubuntu).

using LibUSB;


int main () {

    // declare objects
    Context context;
    Device[] devices;

    // initialize LibUSB and get the device list
    Context.init (out context);
    devices = context.get_device_list ();

    stdout.printf ("\n USB Device List\n---------------\n");

    // iterate through the list
    int i = 0;
    while (devices[i] != null)
    {
        var dev = devices[i];

        // we print all values in hexadecimal here

        stdout.printf ("\n Bus number : %04x", dev.get_bus_number ());
        stdout.printf ("\n Address : %04x", dev.get_device_address ());

        DeviceDescriptor desc = DeviceDescriptor (dev);
        stdout.printf ("\n Vendor ID : %04x",  desc.idVendor);
        stdout.printf ("\n Product ID : %04x", desc.idProduct);

        stdout.printf ("\n");
        i++;
    }


    return 0;
}

Compile and Run

$ valac --pkg libusb-1.0 usb-sample.vala
$ ./usb-sample


Vala/Examples


2024-10-23 11:37