Giter Club home page Giter Club logo

Comments (1)

kernc avatar kernc commented on June 3, 2024

I don't think those comments have anything to do with xsuspender. The only place we poll battery state is this:

xsuspender/src/events.c

Lines 206 to 254 in d96510f

static
gboolean
is_on_ac_power ()
{
#ifdef __linux__
// Read AC power state from /sys/class/power_supply/*/online (== 1 on AC).
// Should work in most cases. See: https://bugs.debian.org/473629
const char *DIRNAME = "/sys/class/power_supply";
const char *basename;
g_autoptr (GError) err = NULL;
g_autoptr (GDir) dir = g_dir_open (DIRNAME, 0, &err);
if (err) {
g_warning ("Cannot read battery/AC status: %s", err->message);
return FALSE;
}
while ((basename = g_dir_read_name (dir))) {
g_autofree char *filename = g_build_filename (DIRNAME, basename, "online", NULL);
g_autofree char *contents = NULL;
if (! g_file_get_contents (filename, &contents, NULL, NULL))
continue;
if (g_strcmp0 (g_strstrip (contents), "1") == 0)
return TRUE;
}
return FALSE;
#elif defined(__unix__) && defined(BSD) && !defined(__APPLE__)
// On *BSD, run `apm -a` which returns '1' when AC is online
g_autoptr (GError) err = NULL;
g_autofree char *standard_output = NULL;
char *argv[] = {"apm", "-a", NULL};
g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL, &standard_output, NULL, NULL, &err);
if (err)
g_warning ("Unexpected `apm -a` execution error: %s", err->message);
return standard_output && 0 == g_strcmp0 (g_strstrip (standard_output), "1");
#else
#warning "No battery / AC status support for your platform."
#warning "Defaulting to as if 'always on battery' behavior. Patches welcome!"
return FALSE;
#endif
}

And this using a fairly high-level and read-only API.

from xsuspender.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.