Determining the default sensor for a type

Multiple Sensors Can Exist for a Type

Sensors was designed so that multiple sensors could exist for a given type. Why? Consider this example.

An Android device has an accelerometer built-in. It also features bluetooth and can pair with a gaming controller that features an accelerometer. To a developer writing a game these two devices are conceptually the same type.

Default Sensor for a Type

To avoid the need to know (or check) what the default sensor for a type is, the system will use the default sensor for a type. Most of the time this is what the app developer wants to do. If the app developer wants to select a specific sensor, he needs to call the QSensor::setIdentifier() method before starting the sensor so that the appropriate backend is used.

From a system perspective though, selecting which sensor should be the default gets tricky. The sensors library uses the first registered identifier as the default. This means that the order in which the sensor backends are registered, is important so the system will allow a config file to determine the default instead.

Sensors.conf

The config file that determines the default sensor for a type is called Sensors.conf. The configuration file is looked for from QtProject directory under the directories given by QStandardPaths::standardLocations(QStandardPaths::ConfigLocation). An example of a complete file path is:

 /etc/xdg/QtProject/Sensors.conf

The first found configuration file is used.

The configuration file has the standard formatting of an ini file. The settings live in the Default group and the general format is:

 type = identifier

An example: Sensors.conf ensures that the sensorfw accelerometer is used by default, ignoring the order in which backends were registered.

 [Default]
 QAccelerometer=sensorfw.accelerometer

If Sensors.conf specifies an identifier that is not registered, the system will fall back to the first registered identifier as the default.

Note that there is a special case logic to prevent the generic plugin's backends from becoming the default when another backend is registered for the same type. This logic means that a backend identifier starting with generic. will only be the default if no other backends have been registered for that type, or if it is specified in Sensors.conf.