Example #1
0
bool PowerX11::GetPowerInfo_Linux_proc_acpi() {
	String node;
	DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
	bool have_battery = false;
	bool have_ac = false;
	bool charging = false;

	this->nsecs_left = -1;
	this->percent_left = -1;
	this->power_state = OS::POWERSTATE_UNKNOWN;

	dirp->change_dir(proc_acpi_battery_path);
	Error err = dirp->list_dir_begin();

	if (err != OK) {
		return false; /* can't use this interface. */
	} else {
		node = dirp->get_next();
		while (node != "") {
			check_proc_acpi_battery(node.utf8().get_data(), &have_battery, &charging /*, seconds, percent*/);
			node = dirp->get_next();
		}
		memdelete(dirp);
	}

	dirp->change_dir(proc_acpi_ac_adapter_path);
	err = dirp->list_dir_begin();
	if (err != OK) {
		return false; /* can't use this interface. */
	} else {
		node = dirp->get_next();
		while (node != "") {
			check_proc_acpi_ac_adapter(node.utf8().get_data(), &have_ac);
			node = dirp->get_next();
		}
		memdelete(dirp);
	}

	if (!have_battery) {
		this->power_state = OS::POWERSTATE_NO_BATTERY;
	} else if (charging) {
		this->power_state = OS::POWERSTATE_CHARGING;
	} else if (have_ac) {
		this->power_state = OS::POWERSTATE_CHARGED;
	} else {
		this->power_state = OS::POWERSTATE_ON_BATTERY;
	}

	return true; /* definitive answer. */
}
Example #2
0
SDL_bool
SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
                                 int *seconds, int *percent)
{
    struct dirent *dent = NULL;
    DIR *dirp = NULL;
    SDL_bool have_battery = SDL_FALSE;
    SDL_bool have_ac = SDL_FALSE;
    SDL_bool charging = SDL_FALSE;

    *seconds = -1;
    *percent = -1;
    *state = SDL_POWERSTATE_UNKNOWN;

    dirp = opendir(proc_acpi_battery_path);
    if (dirp == NULL) {
        return SDL_FALSE;  /* can't use this interface. */
    } else {
        while ((dent = readdir(dirp)) != NULL) {
            const char *node = dent->d_name;
            check_proc_acpi_battery(node, &have_battery, &charging,
                                    seconds, percent);
        }
        closedir(dirp);
    }

    dirp = opendir(proc_acpi_ac_adapter_path);
    if (dirp == NULL) {
        return SDL_FALSE;  /* can't use this interface. */
    } else {
        while ((dent = readdir(dirp)) != NULL) {
            const char *node = dent->d_name;
            check_proc_acpi_ac_adapter(node, &have_ac);
        }
        closedir(dirp);
    }

    if (!have_battery) {
        *state = SDL_POWERSTATE_NO_BATTERY;
    } else if (charging) {
        *state = SDL_POWERSTATE_CHARGING;
    } else if (have_ac) {
        *state = SDL_POWERSTATE_CHARGED;
    } else {
        *state = SDL_POWERSTATE_ON_BATTERY;
    }

    return SDL_TRUE;   /* definitive answer. */
}
Example #3
0
File: syspower.c Project: CCQIU/CC
bool_t CC_CALL sys_get_power_info_acpi(CC_POWER_STATE_ENUM *state, dword_t *seconds, byte_t *percent)
{
    struct dirent *dent = NULL;
    DIR *dirp = NULL;
    bool_t have_battery = FALSE;
    bool_t have_ac = FALSE;
    bool_t charging = FALSE;

    *seconds = -1;
    *percent = -1;
    *state = CC_POWERSTATE_UNKNOWN;

    dirp = opendir(proc_acpi_battery_path);
    if (dirp == NULL) {
        return FALSE;  /* can't use this interface. */
    } else {
        while ((dent = readdir(dirp)) != NULL) {
            const char *node = dent->d_name;
            check_proc_acpi_battery(node, &have_battery, &charging, seconds, percent);
        }
        closedir(dirp);
    }

    dirp = opendir(proc_acpi_ac_adapter_path);
    if (dirp == NULL) {
        return FALSE;  /* can't use this interface. */
    } else {
        while ((dent = readdir(dirp)) != NULL) {
            const char *node = dent->d_name;
            check_proc_acpi_ac_adapter(node, &have_ac);
        }
        closedir(dirp);
    }

    if (!have_battery) {
        *state = CC_POWERSTATE_NO_BATTERY;
    } else if (charging) {
        *state = CC_POWERSTATE_CHARGING;
    } else if (have_ac) {
        *state = CC_POWERSTATE_CHARGED;
    } else {
        *state = CC_POWERSTATE_ON_BATTERY;
    }

    return TRUE;   /* definitive answer. */
}