Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Created December 12, 2015 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Papierkorb/1d543bd8934d79378b1d to your computer and use it in GitHub Desktop.
Save Papierkorb/1d543bd8934d79378b1d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'libusb'
require 'Qt4'
require 'singleton'
class UsbManager
include Singleton
def context
@context ||= LIBUSB::Context.new
end
def devices
context.devices
end
end
class MainWindow < Qt::Widget
def initialize(parent = nil)
super parent
@layout = Qt::VBoxLayout.new
@device_table = Qt::TableWidget.new 0, 4
@device_table.horizontalHeaderLabels = [ tr('Type'), tr('Bus'), tr('ID'), tr('Name') ]
@device_table.horizontalHeader.setResizeMode 3, Qt::HeaderView::Stretch
@device_table.verticalHeader.hide
populate_device_table
setWindowTitle "QEMU USB Control"
@layout.addWidget @device_table
setLayout @layout
end
def populate_device_table
devices = UsbManager.instance.devices
@device_table.rowCount = devices.size
devices.each_with_index do |device, row|
setting = device.interfaces.first.settings.first
type = LIBUSB.dev_string setting.bInterfaceClass, setting.bInterfaceSubClass, setting.bInterfaceProtocol
bus = "#{device.bus_number}/#{device.device_address}"
id = ("%04x:%04x" % [device.idVendor, device.idProduct])
name = device.product.to_s
puts "#{row}: #{type} #{bus} #{id} #{name}"
@device_table.setCellWidget row, 0, Qt::Label.new(type)
@device_table.setCellWidget row, 1, Qt::Label.new(bus)
@device_table.setCellWidget row, 2, Qt::Label.new(id)
@device_table.setCellWidget row, 3, Qt::Label.new(name)
end
end
end
$app = Qt::Application.new ARGV
MainWindow.new.show
$app.exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment