diff options
author | Aylur <[email protected]> | 2024-06-22 20:58:00 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-06-22 20:58:00 +0200 |
commit | 4f3cc1da4aebea5ef1689f56fe6aec950a094bcf (patch) | |
tree | 99db0d2fc908cb93766210a79a5712d4b6c95604 /src | |
parent | 4c8d03265a87ce76cc31fd36eb476d00efe7ef56 (diff) |
remove kbd and powerprofile
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.vala | 30 | ||||
-rw-r--r-- | src/device.vala | 312 | ||||
-rw-r--r-- | src/ifaces.vala | 61 | ||||
-rw-r--r-- | src/kbd.vala | 64 | ||||
-rw-r--r-- | src/meson.build | 3 | ||||
-rw-r--r-- | src/profiles.vala | 65 | ||||
-rw-r--r-- | src/utils.vala | 36 |
7 files changed, 275 insertions, 296 deletions
diff --git a/src/cli.vala b/src/cli.vala index 85e70ae..710edec 100644 --- a/src/cli.vala +++ b/src/cli.vala @@ -37,14 +37,38 @@ int main(string[] argv) { } var battery = AstalBattery.get_default(); - print("%s\n", AstalBattery.to_json(battery)); + print("%s\n", to_json(battery)); if (monitor) { - battery.notify.connect(() => { - print("%s\n", AstalBattery.to_json(battery)); + battery.notify.connect((prop) => { + if (prop.get_name() == "percentage" + || prop.get_name() == "state" + || prop.get_name() == "icon-name" + || prop.get_name() == "time-to-full" + || prop.get_name() == "time-to-empty" + ) { + print("%s\n", to_json(battery)); + } }); new GLib.MainLoop(null, false).run(); } return 0; } + +private string to_json(AstalBattery.Device device) { + string s = "unknown"; + if (device.state == AstalBattery.State.CHARGING) + s = "charging"; + if (device.state == AstalBattery.State.DISCHARGING) + s = "discharging"; + if (device.state == AstalBattery.State.FULLY_CHARGED) + s = "fully_charged"; + + var p = device.percentage; + var i = device.icon_name; + var r = device.state == AstalBattery.State.CHARGING + ? device.time_to_full : device.time_to_empty; + + return "{ \"percentage\": %f, \"state\": \"%s\", \"icon_name\": \"%s\", \"time_remaining\": %f }".printf(p, s, i, r); +} 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"; + } + } +} } diff --git a/src/ifaces.vala b/src/ifaces.vala index 9c769d1..e6eb849 100644 --- a/src/ifaces.vala +++ b/src/ifaces.vala @@ -1,15 +1,6 @@ namespace AstalBattery { -[DBus (name = "org.freedesktop.DBus.Properties")] -public interface Properties : Object { - public signal void properties_changed ( - string iface_name, - HashTable<string, Variant> props, - string[] invalidated_props - ); -} - [DBus (name = "org.freedesktop.UPower")] -interface IUPower : Object { +interface IUPower : DBusProxy { public abstract string[] enumerate_devices() throws Error; public abstract string get_display_device() throws Error; public abstract string get_critical_action() throws Error; @@ -23,24 +14,13 @@ interface IUPower : Object { public abstract bool lis_is_present { get; } } -[DBus (name = "org.freedesktop.UPower.KbdBacklight")] -interface IUPowerKdbBacklight : Object { - public abstract int get_brightness() throws Error; - public abstract int get_max_brightness() throws Error; - public abstract void set_brightness(int value) throws Error; - - public signal void brightness_changed(int value); - public signal void brightness_changed_with_source(int value, string source); -} - [DBus (name = "org.freedesktop.UPower.Device")] -public interface IUPowerDevice : Object { - // public abstract void refresh() throws Error; - // public abstract void get_history(); - // public abstract void get_statistics(); +public interface IUPowerDevice : DBusProxy { + public abstract HistoryDataPoint[] get_history (string type, uint32 timespan, uint32 resolution) throws GLib.Error; + public abstract StatisticsDataPoint[] get_statistics (string type) throws GLib.Error; + public abstract void refresh () throws GLib.Error; - // incompatible with gobject - // public abstract uint type { get; } + public abstract uint Type { get; } public abstract string native_path { owned get; } public abstract string vendor { owned get; } public abstract string model { owned get; } @@ -56,7 +36,7 @@ public interface IUPowerDevice : Object { public abstract double energy_full_design { get; } public abstract double energy_rate { get; } public abstract double voltage { get; } - public abstract int charge_cycles { get; } + public abstract int32 charge_cycles { get; } public abstract double luminosity { get; } public abstract int64 time_to_empty { get; } public abstract int64 time_to_full { get; } @@ -65,26 +45,21 @@ public interface IUPowerDevice : Object { public abstract bool is_present { get; } public abstract uint state { get; } public abstract bool is_rechargable { get; } - public abstract double capacitiy { get; } + public abstract double capacity { get; } public abstract uint technology { get; } - public abstract uint warning_level { get; } - public abstract uint battery_level { get; } + public abstract uint32 warning_level { get; } + public abstract uint32 battery_level { get; } public abstract string icon_name { owned get; } } -[DBus (name = "org.freedesktop.UPower.PowerProfiles")] -public interface IPowerProfiles : Object { - public abstract string[] actions { owned get; } - public abstract string active_profile { owned get; } - public abstract HashTable<string, Variant>[] active_profile_holds { owned get; } - public abstract string performance_degraded { owned get; } - public abstract string performance_inhibited { owned get; } - public abstract HashTable<string, Variant>[] profiles { owned get; } - public abstract string version { owned get; } - - public signal uint profile_released (uint cookie); +public struct HistoryDataPoint { + uint32 time; + double value; + uint32 state; +} - public abstract uint hold_profile(string profile, string reason, string application_id) throws Error; - public abstract void release_profile(uint cookie) throws Error; +public struct StatisticsDataPoint { + double value; + double accuracy; } } diff --git a/src/kbd.vala b/src/kbd.vala deleted file mode 100644 index 13d722e..0000000 --- a/src/kbd.vala +++ /dev/null @@ -1,64 +0,0 @@ -namespace AstalBattery { -public KbdBacklight get_keyboard() { - return KbdBacklight.get_default(); -} - -public class KbdBacklight : Object { - private IUPowerKdbBacklight proxy; - - private static KbdBacklight instance; - public static KbdBacklight? get_default() { - if (instance != null) - return instance; - - try { - instance = new KbdBacklight(); - return instance; - } catch (Error error) { - critical(error.message); - return null; - } - } - - public int brightness { - get { - try { - return proxy.get_brightness(); - } catch (Error error) { - critical(error.message); - return 0; - } - } - set { - try { - proxy.set_brightness(value); - } catch (Error error) { - critical(error.message); - } - } - } - - public int max_brightness { - get { - try { - return proxy.get_max_brightness(); - } catch (Error error) { - critical(error.message); - return 0; - } - } - } - - public KbdBacklight() throws Error { - proxy = Bus.get_proxy_sync( - BusType.SYSTEM, - "org.freedesktop.UPower", - "/org/freedesktop/UPower/KbdBacklight" - ); - - proxy.brightness_changed.connect(() => { - notify_property("brightness"); - }); - } -} -} diff --git a/src/meson.build b/src/meson.build index 9de2aad..6435f31 100644 --- a/src/meson.build +++ b/src/meson.build @@ -26,9 +26,6 @@ sources = [ 'ifaces.vala', 'device.vala', 'upower.vala', - 'kbd.vala', - 'profiles.vala', - 'utils.vala', ] if get_option('lib') diff --git a/src/profiles.vala b/src/profiles.vala deleted file mode 100644 index 4b43923..0000000 --- a/src/profiles.vala +++ /dev/null @@ -1,65 +0,0 @@ -namespace AstalBattery { -public PowerProfiles get_power_profiles() { - return PowerProfiles.get_default(); -} - -public class PowerProfiles : Object { - private static PowerProfiles instance; - public static PowerProfiles get_default() { - if (instance != null) - return instance; - - instance = new PowerProfiles(); - return instance; - } - - private IPowerProfiles proxy; - private Properties props; - - public string[] actions { owned get { return proxy.actions; } } - public string active_profile { owned get { return proxy.active_profile; } } - public HashTable<string, Variant>[] active_profile_holds { owned get { return proxy.active_profile_holds; } } - public string performance_degraded { owned get { return proxy.performance_degraded; } } - public string performance_inhibited { owned get { return proxy.performance_inhibited; } } - public HashTable<string, Variant>[] profiles { owned get { return proxy.profiles; } } - public string version { owned get { return proxy.version; } } - - public signal uint profile_released (uint cookie); - - public uint hold_profile(string profile, string reason, string application_id) throws Error { - return proxy.hold_profile(profile, reason, application_id); - } - - public void release_profile(uint cookie) throws Error { - proxy.release_profile(cookie); - } - - construct { - try { - proxy = Bus.get_proxy_sync( - BusType.SYSTEM, - "org.freedesktop.UPower.PowerProfiles", - "/org/freedesktop/UPower/PowerProfiles" - ); - - props = Bus.get_proxy_sync( - BusType.SYSTEM, - "org.freedesktop.UPower.PowerProfiles", - "/org/freedesktop/UPower/PowerProfiles" - ); - - 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); - } - }); - - proxy.profile_released.connect((cookie) => profile_released(cookie)); - } catch (Error error) { - critical(error.message); - } - } -} -} diff --git a/src/utils.vala b/src/utils.vala deleted file mode 100644 index de22f18..0000000 --- a/src/utils.vala +++ /dev/null @@ -1,36 +0,0 @@ -namespace AstalBattery{ -internal string pascal_to_kebab_case(string pascal) { - StringBuilder kebab = new StringBuilder(); - - for (int i = 0; i < pascal.length; i++) { - char c = pascal[i]; - if (c.isupper()) { - if (i != 0) - kebab.append_c('-'); - - kebab.append_c(c.tolower()); - } else { - kebab.append_c(c); - } - } - - return kebab.str; -} - -internal string to_json(AstalBattery.Device device) { - string s = "unknown"; - if (device.state == AstalBattery.DeviceState.CHARGING) - s = "charging"; - if (device.state == AstalBattery.DeviceState.DISCHARGING) - s = "discharging"; - if (device.state == AstalBattery.DeviceState.FULLY_CHARGED) - s = "fully_charged"; - - var p = device.percentage; - var i = device.icon_name; - var r = device.state == AstalBattery.DeviceState.CHARGING - ? device.time_to_full : device.time_to_empty; - - return "{ \"percentage\": %f, \"state\": \"%s\", \"icon_name\": \"%s\", \"time_remaining\": %f }".printf(p, s, i, r); -} -} |