Giter Club home page Giter Club logo

Comments (5)

kernc avatar kernc commented on June 27, 2024 1

Thanks. IIUC, err should be reset to NULL in each while loop iteration before this line:

if (! g_file_get_contents (filename, &contents, NULL, &err)) {

from xsuspender.

vrossum avatar vrossum commented on June 27, 2024

So how to do that?

(PS your thumbnail looks like a swastika!)

from xsuspender.

vrossum avatar vrossum commented on June 27, 2024

Ok this works:
-const char *DIRNAME = "/sys/class/power_supply";
+const char *DIRNAME = "/sys/class/power_supply/AC";

-g_autofree char *filename = g_build_filename (DIRNAME, basename, "online", NULL);
+g_autofree char *filename = g_build_filename (DIRNAME, "online", NULL);

from xsuspender.

vrossum avatar vrossum commented on June 27, 2024

This works:
(only check sys/class/power_supply/AC/online)

static
gboolean
is_on_ac_power ()
{
#ifdef __linux__
    // Read AC power state from /sys/class/power_supply/AC/online (== 1 on AC).
    // Should work in most cases. See: https://bugs.debian.org/473629

    const char *DIRNAME = "/sys/class/power_supply/AC";
    g_autoptr (GError) err = NULL;

	g_autofree char *filename = g_build_filename (DIRNAME, "online", NULL);
	g_autofree char *contents = NULL;

	g_debug ("Reading '%s'", filename);
	if (! g_file_get_contents (filename, &contents, NULL, &err)) {
	    g_debug ("Cannot read '%s': %s", filename, err->message);
	}

	if (g_strcmp0 (g_strstrip (contents), "1") == 0)
	    return TRUE;
    
    #warning "No battery / AC status support for your platform."
    #warning "Defaulting to as if 'always on battery' behavior. Patches welcome!"
    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");
#endif
}

from xsuspender.

smeijer avatar smeijer commented on June 27, 2024

I'm having a similar error on 19.10. When running xsuspender in debug mode, I'm seeing this log over and over:

Reading '/sys/class/power_supply/AC/online'
Reading '/sys/class/power_supply/BAT0/online'
Cannot read '/sys/class/power_supply/BAT0/online': Failed to open file ?/sys/class/power_supply/BAT0/online?: No such file or directory

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.