device: switch the capabilities to an enum

Strings can suffer from locale issues, the official recommendation for DBus
APIs is to use enums here.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2016-03-15 11:21:09 +10:00
parent 0ebbccc6cf
commit ca2f32ea58
4 changed files with 69 additions and 59 deletions

View File

@@ -36,8 +36,7 @@ The **org.freedesktop.ratbag1.Manager** interface provides:
The **org.freedesktop.ratbag1.Device** interface provides:
- Properties:
- Id -> unique ID of this device
- CapSwitchableResolution, CapSwitchableProfile, CapButtonKeys,
CapButtonMacros, CapDefaultProfile -> booleans to signal capabilities
- Capabilities -> array of uints with the capabilities enum from libratbag
- Description -> device name
- Svg -> device SVG name (file only)
- SvgPath -> device SVG name (full absolute path)

View File

@@ -77,9 +77,17 @@ def find_button(r, args):
def show_device(r, args):
d = find_device(r, args)
caps = { RatbagdDevice.CAP_SWITCHABLE_RESOLUTION : "switchable-resolution",
RatbagdDevice.CAP_SWITCHABLE_PROFILE : "switchable-profile",
RatbagdDevice.CAP_BUTTON_KEY : "button-keys",
RatbagdDevice.CAP_BUTTON_MACROS : "button-macros",
RatbagdDevice.CAP_DEFAULT_PROFILE : "default-profile" }
capabilities = [caps[c] for c in d.capabilities]
print("{} - {}".format(d.id, d.description))
print(" SVG: {}".format(d.svg))
print(" Capabilities: {}".format(", ".join(d.capabilities)))
print(" Capabilities: {}".format(", ".join(capabilities)))
print("Number of Profiles: {}".format(len(d.profiles)))
active = -1
for i, p in enumerate(d.profiles):

View File

@@ -76,6 +76,12 @@ class Ratbagd(_RatbagdDBus):
return self._devices
class RatbagdDevice(_RatbagdDBus):
CAP_SWITCHABLE_RESOLUTION = 1
CAP_SWITCHABLE_PROFILE = 2
CAP_BUTTON_KEY = 3
CAP_BUTTON_MACROS = 4
CAP_DEFAULT_PROFILE = 5
"""
Represents a ratbagd device.
"""
@@ -93,16 +99,7 @@ class RatbagdDevice(_RatbagdDBus):
self._profiles = [RatbagdProfile(objpath) for objpath in result]
self._active_profile = self.dbus_property("ActiveProfile")
caps = [ "switchable-resolution",
"switchable-profile",
"button-keys",
"button-macros",
"default-profile"]
self._caps = []
for c in caps:
cname = "Cap" + "".join([n.capitalize() for n in c.split("-")])
if self.dbus_property(cname):
self._caps.append(c)
self._caps = self.dbus_property("Capabilities")
@property
def profiles(self):
@@ -154,14 +151,10 @@ class RatbagdDevice(_RatbagdDBus):
@property
def capabilities(self):
"""
Return the capabilities of this device as a list of identifier strings.
Return the capabilities of this device as an array.
Capabilities not present on the device are not in the list. Thus use
if "capability" is in deivce.caps:
if "capability" is in device.caps:
do something
Capabilities supported: "switchable-resolution",
"switchable-profile", "button-keys", "button-macros",
"default-profile"
"""
return self._caps

View File

@@ -47,14 +47,6 @@ struct ratbagd_device {
sd_bus_slot *profile_enum_slot;
unsigned int n_profiles;
struct ratbagd_profile **profiles;
struct {
bool switchable_resolution;
bool switchable_profile;
bool button_keys;
bool button_macros;
bool default_profile;
} capabilities;
};
#define ratbagd_device_from_node(_ptr) \
@@ -272,24 +264,59 @@ static int ratbagd_device_get_profile_by_index(sd_bus_message *m,
ratbagd_profile_get_path(profile));
}
static int
ratbagd_device_get_capabilities(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_device *device = userdata;
struct ratbag_device *lib_device = device->lib_device;
enum ratbag_device_capability cap;
int r;
r = sd_bus_message_open_container(reply, 'a', "u");
if (r < 0)
return r;
cap = RATBAG_DEVICE_CAP_SWITCHABLE_RESOLUTION;
if (ratbag_device_has_capability(lib_device, cap)) {
r = sd_bus_message_append(reply, "u", cap);
if (r < 0)
return r;
}
cap = RATBAG_DEVICE_CAP_SWITCHABLE_PROFILE;
if (ratbag_device_has_capability(lib_device, cap)) {
r = sd_bus_message_append(reply, "u", cap);
if (r < 0)
return r;
}
cap = RATBAG_DEVICE_CAP_BUTTON_MACROS;
if (ratbag_device_has_capability(lib_device, cap)) {
r = sd_bus_message_append(reply, "u", cap);
if (r < 0)
return r;
}
cap = RATBAG_DEVICE_CAP_DEFAULT_PROFILE;
if (ratbag_device_has_capability(lib_device, cap)) {
r = sd_bus_message_append(reply, "u", cap);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
const sd_bus_vtable ratbagd_device_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Id", "s", NULL, offsetof(struct ratbagd_device, name), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CapSwitchableResolution", "b", NULL,
offsetof(struct ratbagd_device, capabilities.switchable_resolution),
SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CapSwitchableProfile", "b", NULL,
offsetof(struct ratbagd_device, capabilities.switchable_profile),
SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CapButtonKeys", "b", NULL,
offsetof(struct ratbagd_device, capabilities.button_keys),
SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CapButtonMacros", "b", NULL,
offsetof(struct ratbagd_device, capabilities.button_macros),
SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CapDefaultProfile", "b", NULL,
offsetof(struct ratbagd_device, capabilities.default_profile),
SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Capabilities", "au", ratbagd_device_get_capabilities, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Description", "s", ratbagd_device_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Svg", "s", ratbagd_device_get_svg, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SvgPath", "s", ratbagd_device_get_svg_path, 0, SD_BUS_VTABLE_PROPERTY_CONST),
@@ -358,23 +385,6 @@ int ratbagd_device_new(struct ratbagd_device **out,
}
}
device->capabilities.switchable_resolution =
ratbag_device_has_capability(lib_device,
RATBAG_DEVICE_CAP_SWITCHABLE_RESOLUTION);
device->capabilities.switchable_profile =
ratbag_device_has_capability(lib_device,
RATBAG_DEVICE_CAP_SWITCHABLE_PROFILE);
device->capabilities.button_keys =
ratbag_device_has_capability(lib_device,
RATBAG_DEVICE_CAP_BUTTON_MACROS);
device->capabilities.button_macros =
ratbag_device_has_capability(lib_device,
RATBAG_DEVICE_CAP_BUTTON_MACROS);
device->capabilities.default_profile =
ratbag_device_has_capability(lib_device,
RATBAG_DEVICE_CAP_DEFAULT_PROFILE);
*out = device;
device = NULL;
return 0;