Development/Tutorials/Solid/Introduction: Difference between revisions
No edit summary |
|||
Line 31: | Line 31: | ||
This second program makes uses of filters built into the solid framework. Using these filters you can list only devices according to supported capabilities, sub-devices of a given parent, and various predicates. In our example we'll be limiting our list to only audio hardware. A full list of capabilities can be viewed under the Solid::Capability namespace. | This second program makes uses of filters built into the solid framework. Using these filters you can list only devices according to supported capabilities, sub-devices of a given parent, and various predicates. In our example we'll be limiting our list to only audio hardware. A full list of capabilities can be viewed under the Solid::Capability namespace. | ||
<code cppqt>Solid::DeviceManager &manager = Solid::DeviceManager::self(); | <code cppqt> | ||
Solid::DeviceManager &manager = Solid::DeviceManager::self(); | |||
//get a list of all devices that are AudioHw | //get a list of all devices that are AudioHw | ||
foreach ( Solid::Device device, | foreach(Solid::Device device, manager.findDevicesFromQuery(Solid::Capability::AudioHw, QString()) ) | ||
{ | { | ||
kDebug() << device.udi().toLatin1().constData() << endl; | |||
} | } | ||
</code> | </code> | ||
Line 43: | Line 43: | ||
In this example Solid::DeviceManager::findDevicesFromQuery looks for a device with any parent and the Solid::Capability::AudioHw capability. If we had wanted to specify an AudioHw device with the parent "real_specific_parent" it would look like this: | In this example Solid::DeviceManager::findDevicesFromQuery looks for a device with any parent and the Solid::Capability::AudioHw capability. If we had wanted to specify an AudioHw device with the parent "real_specific_parent" it would look like this: | ||
<code cppqt>manager.findDevicesFromQuery( | <code cppqt>manager.findDevicesFromQuery(Solid::Capability::AudioHw,"real_specific_parent" )</code> | ||
The complete program along with the CMake files required to build it can be found under "[http://websvn.kde.org/trunk/KDE/kdelibs/solid/examples/tutorial2/ kdelibs/solid/examples/tutorial2/]". | The complete program along with the CMake files required to build it can be found under "[http://websvn.kde.org/trunk/KDE/kdelibs/solid/examples/tutorial2/ kdelibs/solid/examples/tutorial2/]". | ||
Revision as of 01:13, 10 April 2007
What is Solid
Solid is the new hardware device framework for KDE 4 that features, among other things, a hardware discovery layer which allows you to detect and use devices regardless of operating system or architecture. You can learn more about the Solid project at solid.kde.org.
Who is this tutorial for?
This tutorial is written for developers looking to use the Solid hardware discovery layer within their applications. It can also serve as a good starting point for developers looking to start working on the Solid framework.
Prerequisites
- A working KDE4 development environment. Documentation on setting up a KDE environment can be found here.
Listing Devices
Our first program will be a simple console based app that gets a list of all the hardware devices and prints them out to the screen.
Solid::DeviceManager &manager = Solid::DeviceManager::self();
This gets us the device manager. All the devices are queried through and returned from the device manager. Once we have the list of devices we can interact with them as follows:
foreach( Solid::Device device, manager.allDevices() )
{
//print the name of device
kDebug() << device.udi() << endl;
}
device.udi() returns the Unique Device Identifier for the device as a QString. Even if you have more than one identical device, the UDI is guaranteed to be unique. For example if you have a MythTV box with two PVR-250 T.V. capture cards in it, you will be able to refer to card #1 and #2 by their respective UDI.
The complete program along with the CMake files necessary to build it can be found under "kdelibs/solid/examples/tutorial1/".
Searching for specific devices
This second program makes uses of filters built into the solid framework. Using these filters you can list only devices according to supported capabilities, sub-devices of a given parent, and various predicates. In our example we'll be limiting our list to only audio hardware. A full list of capabilities can be viewed under the Solid::Capability namespace.
Solid::DeviceManager &manager = Solid::DeviceManager::self();
//get a list of all devices that are AudioHw
foreach(Solid::Device device, manager.findDevicesFromQuery(Solid::Capability::AudioHw, QString()) )
{
kDebug() << device.udi().toLatin1().constData() << endl;
}
In this example Solid::DeviceManager::findDevicesFromQuery looks for a device with any parent and the Solid::Capability::AudioHw capability. If we had wanted to specify an AudioHw device with the parent "real_specific_parent" it would look like this:
manager.findDevicesFromQuery(Solid::Capability::AudioHw,"real_specific_parent" )
The complete program along with the CMake files required to build it can be found under "kdelibs/solid/examples/tutorial2/".
What do we do with a device once we get it?
Now that we got a device, what do we do with it? First let's look at the relationship between the Solid::Device and Solid::Capability. A Solid::Device can contain many Solid::Capability. A device can be tested to have a capability in the following way:
Solid::DeviceManager &manager = Solid::DeviceManager::self();
//get a Processor
Solid::DeviceList list = manager.findDevicesFromQuery( "",
Solid::Capability::Processor );
//take the first Processor
Solid::Device device = list[0];
if ( device.is<Solid::Processor>() ) {
kDebug() << "We've got a processor!" << endl;
} else {
kDebug() << "Device is not a processor." << endl;
}
To actually use this device as a processor we need to do the following:
Solid::Processor *processor = device.as<Solid::Processor>();
The complete program along with the CMake files required to build it can be found under "kdelibs/solid/examples/tutorial3/".