diff options
Diffstat (limited to 'src/device.vala')
-rw-r--r-- | src/device.vala | 312 |
1 files changed, 230 insertions, 82 deletions
diff --git a/src/device.vala b/src/device.vala index 2dc03eb..eab3770 100644 --- a/src/device.vala +++ b/src/device.vala @@ -10,8 +10,7 @@ public class Device : Object { return display_device; try { - display_device = new Device( - "/org/freedesktop/UPower/devices/DisplayDevice"); + display_device = new Device("/org/freedesktop/UPower/devices/DisplayDevice"); return display_device; } catch (Error error) { @@ -20,94 +19,120 @@ public class Device : Object { return null; } - private Properties props; private IUPowerDevice proxy; - public DeviceType device_type { - get { - Value value = Value(Type.UINT); - proxy.get_property("name", ref value); - return value.get_uint(); - } + public Device(string path) throws Error { + proxy = Bus.get_proxy_sync(BusType.SYSTEM, "org.freedesktop.UPower", path); + proxy.g_properties_changed.connect(sync); + sync(); } - public string native_path { owned get { return proxy.native_path; } } - public string vendor { owned get { return proxy.vendor; } } - public string model { owned get { return proxy.model; } } - public string serial { owned get { return proxy.serial; } } - public uint64 update_time { get { return proxy.update_time; } } - public bool power_supply { get { return proxy.power_supply; } } - public bool has_history { get { return proxy.has_history; } } - public bool has_statistics { get { return proxy.has_statistics; } } - public bool online { get { return proxy.online; } } - public double energy { get { return proxy.energy; } } - public double energy_empty { get { return proxy.energy_empty; } } - public double energy_full { get { return proxy.energy_full; } } - public double energy_full_design { get { return proxy.energy_full_design; } } - public double energy_rate { get { return proxy.energy_rate; } } - public double voltage { get { return proxy.voltage; } } - public int charge_cycles { get { return proxy.charge_cycles; } } - public double luminosity { get { return proxy.luminosity; } } - public int64 time_to_empty { get { return proxy.time_to_empty; } } - public int64 time_to_full { get { return proxy.time_to_full; }} - public double percentage { get { return proxy.percentage / 100; } } - public double temperature { get { return proxy.temperature; } } - public bool is_present { get { return proxy.is_present; } } - public DeviceState state { get { return proxy.state; } } - public bool is_rechargable { get { return proxy.is_rechargable; } } - public double capacitiy { get { return proxy.capacitiy; } } - public DeviceTechnology technology { get { return proxy.technology; } } - public WarningLevel warning_level { get { return proxy.warning_level; } } - public BatteryLevel battery_level { get { return proxy.battery_level; } } - public string icon_name { owned get { return proxy.icon_name; } } + public Type device_type { get; private set; } + public string native_path { owned get; private set; } + public string vendor { owned get; private set; } + public string model { owned get; private set; } + public string serial { owned get; private set; } + public uint64 update_time { get; private set; } + public bool power_supply { get; private set; } + public bool has_history { get; private set; } + public bool has_statistics { get; private set; } + public bool online { get; private set; } + public double energy { get; private set; } + public double energy_empty { get; private set; } + public double energy_full { get; private set; } + public double energy_full_design { get; private set; } + public double energy_rate { get; private set; } + public double voltage { get; private set; } + public int charge_cycles { get; private set; } + public double luminosity { get; private set; } + public int64 time_to_empty { get; private set; } + public int64 time_to_full { get; private set;} + public double percentage { get; private set; } + public double temperature { get; private set; } + public bool is_present { get; private set; } + public State state { get; private set; } + public bool is_rechargable { get; private set; } + public double capacity { get; private set; } + public Technology technology { get; private set; } + public WarningLevel warning_level { get; private set; } + public BatteryLevel battery_level { get; private set; } + public string icon_name { owned get; private set; } - public Device(string path) throws Error { - proxy = Bus.get_proxy_sync(BusType.SYSTEM, "org.freedesktop.UPower", path); - props = Bus.get_proxy_sync(BusType.SYSTEM, "org.freedesktop.UPower", path); - - props.properties_changed.connect((iface, vardict) => { - foreach (var key in vardict.get_keys()) { - var prop = pascal_to_kebab_case(key); - if (get_class().find_property(prop) != null) - notify_property(prop); - } - }); + public bool charging { get; private set; } + public bool is_battery { get; private set; } + public string battery_icon_name { get; private set; } + public string device_type_name { get; private set; } + public string device_type_icon { get; private set; } + + private unowned string get_battery_icon() { + if (percentage <= 0) + return "battery-good"; + + if (percentage < 0.10) + return "battery-empty"; + + if (percentage < 0.37) + return "battery-caution"; + + if (percentage < 0.62) + return "battery-low"; + + if (percentage < 0.87) + return "battery-good"; + + return "battery-full"; } -} -public enum DeviceType { - UNKNOWN = 0, - LINE_POWER, - BATTERY, - UPS, - MONITOR, - MOUSE, - KEYBOARD, - PDA, - PHONE, - MEDIA_PLAYER, - TABLET, - COMPUTER, - GAMING_INPUT, - PEN, - TOUCHPAD, - MODEM, - NETWORK, - HEADSET, - SPEAKERS, - HEADPHONES, - VIDEO, - OTHER_AUDIO, - REMOVE_CONTROL, - PRINTER, - SCANNER, - CAMERA, - WEARABLE, - TOY, - BLUETOOTH_GENERIC, + + public void sync() { + device_type = (Type)proxy.Type; + native_path = proxy.native_path; + vendor = proxy.vendor; + model = proxy.model; + serial = proxy.serial; + update_time = proxy.update_time; + power_supply = proxy.power_supply; + has_history = proxy.has_history; + has_statistics = proxy.has_statistics; + online = proxy.online; + energy = proxy.energy; + energy_empty = proxy.energy_empty; + energy_full = proxy.energy_full; + energy_full_design = proxy.energy_full_design; + energy_rate = proxy.energy_rate; + voltage = proxy.voltage; + charge_cycles = proxy.charge_cycles; + luminosity = proxy.luminosity; + time_to_empty = proxy.time_to_empty; + time_to_full = proxy.time_to_full; + percentage = proxy.percentage / 100; + temperature = proxy.temperature; + is_present = proxy.is_present; + state = (State)proxy.state; + is_rechargable = proxy.is_rechargable; + capacity = proxy.capacity; + technology = (Technology)proxy.technology; + warning_level = (WarningLevel)proxy.warning_level; + battery_level = (BatteryLevel)proxy.battery_level; + icon_name = proxy.icon_name; + + charging = state == State.FULLY_CHARGED || state == State.CHARGING; + is_battery = device_type != Type.UNKNOWN && device_type != Type.LINE_POWER; + + if (!is_battery) + battery_icon_name = "preferences-system-power"; + else if (percentage == 1.0 && charging) + battery_icon_name = "battery-full-charged"; + else + battery_icon_name = charging ? get_battery_icon() + "-charging" : get_battery_icon(); + + device_type_name = device_type.get_name(); + device_type_icon = device_type.get_icon_name(); + } } -public enum DeviceState { +[CCode (type_signature = "u")] +public enum State { UNKNOWN = 0, CHARGING, DISCHARGING, @@ -117,7 +142,8 @@ public enum DeviceState { PENDING_DISCHARGE, } -public enum DeviceTechnology { +[CCode (type_signature = "u")] +public enum Technology { UNKNOWN = 0, LITHIUM_ION, LITHIUM_POLYMER, @@ -127,6 +153,7 @@ public enum DeviceTechnology { NICKEL_METAL_HYDRIDE, } +[CCode (type_signature = "u")] public enum WarningLevel { UNKNOWN = 0, NONE, @@ -136,6 +163,7 @@ public enum WarningLevel { ACTION, } +[CCode (type_signature = "u")] public enum BatteryLevel { UNKNOWN = 0, NONE, @@ -145,4 +173,124 @@ public enum BatteryLevel { HIGH, FULL, } + +[CCode (type_signature = "u")] +public enum Type { + UNKNOWN = 0, + LINE_POWER, + BATTERY, + UPS, + MONITOR, + MOUSE, + KEYBOARD, + PDA, + PHONE, + MEDIA_PLAYER, + TABLET, + COMPUTER, + GAMING_INPUT, + PEN, + TOUCHPAD, + MODEM, + NETWORK, + HEADSET, + SPEAKERS, + HEADPHONES, + VIDEO, + OTHER_AUDIO, + REMOVE_CONTROL, + PRINTER, + SCANNER, + CAMERA, + WEARABLE, + TOY, + BLUETOOTH_GENERIC; + + // TODO: add more icon names + public string? get_icon_name () { + switch (this) { + case UPS: + return "uninterruptible-power-supply"; + case MOUSE: + return "input-mouse"; + case KEYBOARD: + return "input-keyboard"; + case PDA: + case PHONE: + return "phone"; + case MEDIA_PLAYER: + return "multimedia-player"; + case TABLET: + case PEN: + return "input-tablet"; + case GAMING_INPUT: + return "input-gaming"; + default: + return null; + } + } + + public unowned string? get_name () { + switch (this) { + case LINE_POWER: + return "Plugged In"; + case BATTERY: + return "Battery"; + case UPS: + return "UPS"; + case MONITOR: + return "Display"; + case MOUSE: + return "Mouse"; + case KEYBOARD: + return "Keyboard"; + case PDA: + return "PDA"; + case PHONE: + return "Phone"; + case MEDIA_PLAYER: + return "Media Player"; + case TABLET: + return "Tablet"; + case COMPUTER: + return "Computer"; + case GAMING_INPUT: + return "Controller"; + case PEN: + return "Pen"; + case TOUCHPAD: + return "Touchpad"; + case MODEM: + return "Modem"; + case NETWORK: + return "Network"; + case HEADSET: + return "Headset"; + case SPEAKERS: + return "Speakers"; + case HEADPHONES: + return "Headphones"; + case VIDEO: + return "Video"; + case OTHER_AUDIO: + return "Other Audio"; + case REMOVE_CONTROL: + return "Remove Control"; + case PRINTER: + return "Printer"; + case SCANNER: + return "Scanner"; + case CAMERA: + return "Camera"; + case WEARABLE: + return "Wearable"; + case TOY: + return "Toy"; + case BLUETOOTH_GENERIC: + return "Bluetooth Generic"; + default: + return "Unknown"; + } + } +} } |