Skip to content

Commit

Permalink
Delay the Battery Monitor plugin alarm
Browse files Browse the repository at this point in the history
There is often a short fluctuation in the calculation of the remaining
battery life, so check that the calculation remains stable for about a
minute to avoid false alarms.

Add one minute to the default alarmTime to account for this delay.

This closes github issue #42, reported by cburgard.
  • Loading branch information
ib committed Jan 24, 2025
1 parent 09b2591 commit 0d97457
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions plugins/batt/batt.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Copyright (C) 2014-2019 Andriy Grytsenko <[email protected]>
* 2015 Balló György <[email protected]>
* 2015 Stanislav Kozina, Ersin <[email protected]>
* 2020 Ingo Brückl
* 2020,2025 Ingo Brückl
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -72,6 +72,7 @@ typedef struct {
GtkWidget *drawingArea;
GtkOrientation orientation;
unsigned int alarmTime,
alarmTimeReached,
border,
height,
length,
Expand Down Expand Up @@ -113,7 +114,6 @@ static void * alarmProcess(void *arg) {
sem_wait(a->lock);
if (system(a->command) != 0)
g_warning("plugin batt: failed to execute alarm command \"%s\"", a->command);
sleep(51); /* do not spam messages more often than once a minute */
sem_post(a->lock);

g_free(a);
Expand Down Expand Up @@ -276,6 +276,11 @@ void update_display(lx_battery *lx_b, gboolean repaint) {
if ( !isCharging &&
( ( battery_get_remaining( b ) / 60 ) < (int)lx_b->alarmTime ) )
{
/* make sure it's stable enough for about a minute (6 * 9 seconds) */
if (++lx_b->alarmTimeReached > 6)
{
lx_b->alarmTimeReached = 0;

/* FIXME: this should be done using glibs process functions */
/* FIXME: see bug #463: it should not spawn process all the time */
/* Alarms should not run concurrently; determine whether an alarm is
Expand All @@ -295,7 +300,10 @@ void update_display(lx_battery *lx_b, gboolean repaint) {
pthread_t alarmThread;
pthread_create(&alarmThread, NULL, alarmProcess, a);
}
}
}
else
lx_b->alarmTimeReached = 0;

set_tooltip_text(lx_b);

Expand Down Expand Up @@ -500,7 +508,7 @@ static GtkWidget * constructor(LXPanel *panel, config_setting_t *settings)
= lx_b->dischargingColor1 = lx_b->dischargingColor2 = NULL;

/* Set default values for integers */
lx_b->alarmTime = 5;
lx_b->alarmTime = 6;
lx_b->requestedBorder = 1;
lx_b->thickness = 8;

Expand Down

0 comments on commit 0d97457

Please sign in to comment.