Skip to content

Commit

Permalink
power: Get Charge Profiles over Dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Jan 22, 2021
1 parent 514eba9 commit c9f51cc
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 232 deletions.
120 changes: 28 additions & 92 deletions panels/power/cc-charge-threshold-dialog.c
Original file line number Diff line number Diff line change
@@ -1,63 +1,14 @@
#include "cc-charge-threshold-dialog.h"
#include "cc-charge-threshold-row.h"
#include "list-box-helper.h"

ChargeProfile charge_profile_from_thresholds (guchar start, guchar end) {
if (start == 50 && end == 60)
return CHARGE_PROFILE_MAX_LIFESPAN;
else if (start == 85 && end == 90)
return CHARGE_PROFILE_BALANCED;
else if (start == 96 && end == 100)
return CHARGE_PROFILE_FULL_CHARGE;
else
return CHARGE_PROFILE_UNDEFINED;
}

void charge_profile_get_thresholds (ChargeProfile profile, guchar *start, guchar *end) {
switch (profile) {
case CHARGE_PROFILE_MAX_LIFESPAN:
*start = 50;
*end = 60;
break;
case CHARGE_PROFILE_BALANCED:
*start = 85;
*end = 90;
break;
case CHARGE_PROFILE_FULL_CHARGE:
default:
*start = 96;
*end = 100;
break;
}
}

gchar *charge_profile_title_from_thresholds (guchar start, guchar end) {
switch (charge_profile_from_thresholds (start, end)) {
case CHARGE_PROFILE_MAX_LIFESPAN:
return g_strdup ("Max lifespan");
case CHARGE_PROFILE_BALANCED:
return g_strdup ("Balanced");
case CHARGE_PROFILE_FULL_CHARGE:
return g_strdup ("Full Charge");
default:
return g_strdup_printf ("Custom (%d%% - %d%%)", start, end);
}
}

struct _CcChargeThresholdDialog {
GtkDialog parent_instance;

S76PowerDaemon *power_proxy;
GtkScale *scale;
GtkListBox *listbox;
GtkListBoxRow *max_lifespan_row;
GtkListBoxRow *balanced_row;
GtkListBoxRow *full_charge_row;
GtkRadioButton *max_lifespan_radio;
GtkRadioButton *balanced_radio;
GtkRadioButton *full_charge_radio;
GtkRadioButton *previous_radio;
GtkRadioButton *hidden_radio;
GtkRadioButton *previous_radio;
};

G_DEFINE_TYPE (CcChargeThresholdDialog, cc_charge_threshold_dialog, GTK_TYPE_DIALOG)
Expand Down Expand Up @@ -87,9 +38,9 @@ set_charge_thresholds_ready(GObject *source_object,
static void
radio_toggled_cb (CcChargeThresholdDialog *self, GtkRadioButton *radio)
{
ChargeProfile profile;
guchar start, end;
ChargeProfile *profile;
GVariant *thresholds = NULL;
GtkWidget *row;
gboolean state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio));

if (!state)
Expand All @@ -98,30 +49,22 @@ radio_toggled_cb (CcChargeThresholdDialog *self, GtkRadioButton *radio)
return;
}

if (radio == self->max_lifespan_radio)
profile = CHARGE_PROFILE_MAX_LIFESPAN;
else if (radio == self->balanced_radio)
profile = CHARGE_PROFILE_BALANCED;
else if (radio == self->full_charge_radio)
profile = CHARGE_PROFILE_FULL_CHARGE;
else
row = gtk_widget_get_ancestor (GTK_WIDGET (radio), CC_TYPE_CHARGE_THRESHOLD_ROW);
if (row == NULL)
return;

charge_profile_get_thresholds (profile, &start, &end);
thresholds = g_variant_new ("(yy)", start, end);
profile = cc_charge_threshold_row_get_profile (CC_CHARGE_THRESHOLD_ROW (row));
thresholds = g_variant_new ("(yy)", profile->start, profile->end);

s76_power_daemon_call_set_charge_thresholds (self->power_proxy, thresholds, NULL, set_charge_thresholds_ready, self);
}

static void
row_activated_cb (CcChargeThresholdDialog *self, GtkListBoxRow *row)
{
if (row == self->max_lifespan_row)
gtk_button_clicked (GTK_BUTTON (self->max_lifespan_radio));
else if (row == self->balanced_row)
gtk_button_clicked (GTK_BUTTON (self->balanced_radio));
else if (row == self->full_charge_row)
gtk_button_clicked (GTK_BUTTON (self->full_charge_radio));
CcChargeThresholdRow *profile_row = CC_CHARGE_THRESHOLD_ROW (row);
GtkRadioButton *radio = cc_charge_threshold_row_get_radio (profile_row);
gtk_button_clicked (GTK_BUTTON (radio));
}

static void
Expand All @@ -134,12 +77,6 @@ cc_charge_threshold_dialog_class_init (CcChargeThresholdDialogClass *klass)
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/power/cc-charge-threshold-dialog.ui");

gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, listbox);
gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, max_lifespan_row);
gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, balanced_row);
gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, full_charge_row);
gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, max_lifespan_radio);
gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, balanced_radio);
gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, full_charge_radio);
gtk_widget_class_bind_template_child (widget_class, CcChargeThresholdDialog, hidden_radio);

gtk_widget_class_bind_template_callback (widget_class, radio_toggled_cb);
Expand All @@ -157,33 +94,32 @@ cc_charge_threshold_dialog_init (CcChargeThresholdDialog *self)
}

CcChargeThresholdDialog*
cc_charge_threshold_dialog_new (S76PowerDaemon *power_proxy, ChargeProfile profile)
cc_charge_threshold_dialog_new (S76PowerDaemon *power_proxy, ChargeProfile **profiles, ChargeProfile *profile)
{
GtkRadioButton *radio = NULL;
GtkRadioButton *selected_radio = NULL;
CcChargeThresholdDialog *dialog = g_object_new (CC_TYPE_CHARGE_THRESHOLD_DIALOG,
"use-header-bar", 1,
NULL);
dialog->power_proxy = power_proxy;

switch (profile) {
case CHARGE_PROFILE_MAX_LIFESPAN:
radio = dialog->max_lifespan_radio;
break;
case CHARGE_PROFILE_BALANCED:
radio = dialog->balanced_radio;
break;
case CHARGE_PROFILE_FULL_CHARGE:
radio = dialog->full_charge_radio;
break;
default:
break;
}
for (int i = 0; profiles[i] != NULL; i++)
{
CcChargeThresholdRow *row = cc_charge_threshold_row_new (profiles[i]);
gtk_container_add (GTK_CONTAINER (dialog->listbox), GTK_WIDGET (row));

GtkRadioButton *radio = cc_charge_threshold_row_get_radio (row);
gtk_radio_button_join_group (radio, dialog->hidden_radio);
g_signal_connect_object (radio, "toggled", G_CALLBACK (radio_toggled_cb), dialog, G_CONNECT_SWAPPED);

if (profiles[i] == profile)
selected_radio = radio;
}

if (radio)
if (selected_radio)
{
g_signal_handlers_block_by_func (radio, radio_toggled_cb, dialog);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio), TRUE);
g_signal_handlers_unblock_by_func (radio, radio_toggled_cb, dialog);
g_signal_handlers_block_by_func (selected_radio, radio_toggled_cb, dialog);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (selected_radio), TRUE);
g_signal_handlers_unblock_by_func (selected_radio, radio_toggled_cb, dialog);
}
else
{
Expand Down
13 changes: 2 additions & 11 deletions panels/power/cc-charge-threshold-dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,12 @@

#include <gtk/gtk.h>
#include "cc-system76-power-generated.h"
#include "cc-charge-threshold-row.h"

G_BEGIN_DECLS

typedef enum {
CHARGE_PROFILE_MAX_LIFESPAN,
CHARGE_PROFILE_BALANCED,
CHARGE_PROFILE_FULL_CHARGE,
CHARGE_PROFILE_UNDEFINED,
} ChargeProfile;

#define CC_TYPE_CHARGE_THRESHOLD_DIALOG (cc_charge_threshold_dialog_get_type())
G_DECLARE_FINAL_TYPE (CcChargeThresholdDialog, cc_charge_threshold_dialog, CC, CHARGE_THRESHOLD_DIALOG, GtkDialog)
CcChargeThresholdDialog* cc_charge_threshold_dialog_new (S76PowerDaemon *power_proxy, ChargeProfile profile);
ChargeProfile charge_profile_from_thresholds (guchar start, guchar end);
void charge_profile_get_thresholds (ChargeProfile profile, guchar *start, guchar *end);
gchar *charge_profile_title_from_thresholds (guchar start, guchar end);
CcChargeThresholdDialog* cc_charge_threshold_dialog_new (S76PowerDaemon *power_proxy, ChargeProfile **profiles, ChargeProfile *profile);

G_END_DECLS
40 changes: 0 additions & 40 deletions panels/power/cc-charge-threshold-dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,6 @@
<property name="visible">True</property>
<property name="selection-mode">GTK_SELECTION_BROWSE</property>
<signal name="row-activated" handler="row_activated_cb" object="CcChargeThresholdDialog" swapped="yes" />
<child>
<object class="CcChargeThresholdRow" id="full_charge_row">
<property name="visible">True</property>
<property name="title">Full Charge (100%)</property>
<property name="description">Battery is charged to its full capacity for the longest possible use on battery power. Charging resumes when the battery falls below 96% charge.</property>
<child internal-child="radio">
<object class="GtkRadioButton" id="full_charge_radio">
<property name="group">max_lifespan_radio</property>
<signal name="toggled" handler="radio_toggled_cb" object="CcChargeThresholdDialog" swapped="yes" />
</object>
</child>
</object>
</child>
<child>
<object class="CcChargeThresholdRow" id="balanced_row">
<property name="visible">True</property>
<property name="title">Balanced (90%)</property>
<property name="description">Use this threshold when you unplug frequently but don't need the full battery capacity. Charging stops when the battery reaches 90% capacity and resumes when the battery falls below 85%.</property>
<child internal-child="radio">
<object class="GtkRadioButton" id="balanced_radio">
<property name="group">max_lifespan_radio</property>
<signal name="toggled" handler="radio_toggled_cb" object="CcChargeThresholdDialog" swapped="yes" />
</object>
</child>
</object>
</child>
<child>
<object class="CcChargeThresholdRow" id="max_lifespan_row">
<property name="visible">True</property>
<property name="title">Maximum Lifespan (60%)</property>
<property name="description">Use this threshold if you rarely use the system on battery for extended periods. Charging stops when the battery reaches 60% capacity and resumes when the battery falls below 50%.</property>
<child internal-child="radio">
<object class="GtkRadioButton" id="max_lifespan_radio">
<property name="group">max_lifespan_radio</property>
<signal name="toggled" handler="radio_toggled_cb" object="CcChargeThresholdDialog" swapped="yes" />
</object>
</child>
</object>
</child>
</object>
</child>
</object>
Expand All @@ -73,7 +34,6 @@
<!-- Hack to show no radio button selected when profile is undefined -->
<object class="GtkRadioButton" id="hidden_radio">
<property name="visible">False</property>
<property name="group">max_lifespan_radio</property>
<signal name="toggled" handler="radio_toggled_cb" object="CcChargeThresholdDialog" swapped="yes" />
</object>
</child>
Expand Down
Loading

0 comments on commit c9f51cc

Please sign in to comment.