Example #1
0
static void
check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
{
    const char *base = proc_acpi_ac_adapter_path;
    char state[256];
    char *ptr = NULL;
    char *key = NULL;
    char *val = NULL;

    if (!read_power_file(base, node, "state", state, sizeof (state))) {
        return;
    }

    ptr = &state[0];
    while (make_proc_acpi_key_val(&ptr, &key, &val)) {
        if (strcmp(key, "state") == 0) {
            if (strcmp(val, "on-line") == 0) {
                *have_ac = SDL_TRUE;
            }
        }
    }
}
Example #2
0
void PowerX11::check_proc_acpi_ac_adapter(const char *node, bool *have_ac) {
	const char *base = proc_acpi_ac_adapter_path;
	char state[256];
	char *ptr = NULL;
	char *key = NULL;
	char *val = NULL;

	if (!read_power_file(base, node, "state", state, sizeof(state))) {
		return;
	}

	ptr = &state[0];
	while (make_proc_acpi_key_val(&ptr, &key, &val)) {
		String skey = key;
		if (skey == "state") {
			String sval = val;
			if (sval == "on-line") {
				*have_ac = true;
			}
		}
	}
}
Example #3
0
SDL_bool
SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *seconds, int *percent)
{
    const char *base = sys_class_power_supply_path;
    struct dirent *dent;
    DIR *dirp;

    dirp = opendir(base);
    if (!dirp) {
        return SDL_FALSE;
    }

    *state = SDL_POWERSTATE_NO_BATTERY;  /* assume we're just plugged in. */
    *seconds = -1;
    *percent = -1;

    while ((dent = readdir(dirp)) != NULL) {
        const char *name = dent->d_name;
        SDL_bool choose = SDL_FALSE;
        char str[64];
        SDL_PowerState st;
        int secs;
        int pct;

        if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
            continue;  /* skip these, of course. */
        } else if (!read_power_file(base, name, "type", str, sizeof (str))) {
            continue;  /* Don't know _what_ we're looking at. Give up on it. */
        } else if (SDL_strcmp(str, "Battery\n") != 0) {
            continue;  /* we don't care about UPS and such. */
        }

        /* some drivers don't offer this, so if it's not explicitly reported assume it's present. */
        if (read_power_file(base, name, "present", str, sizeof (str)) && (SDL_strcmp(str, "0\n") == 0)) {
            st = SDL_POWERSTATE_NO_BATTERY;
        } else if (!read_power_file(base, name, "status", str, sizeof (str))) {
            st = SDL_POWERSTATE_UNKNOWN;  /* uh oh */
        } else if (SDL_strcmp(str, "Charging\n") == 0) {
            st = SDL_POWERSTATE_CHARGING;
        } else if (SDL_strcmp(str, "Discharging\n") == 0) {
            st = SDL_POWERSTATE_ON_BATTERY;
        } else if ((SDL_strcmp(str, "Full\n") == 0) || (SDL_strcmp(str, "Not charging\n") == 0)) {
            st = SDL_POWERSTATE_CHARGED;
        } else {
            st = SDL_POWERSTATE_UNKNOWN;  /* uh oh */
        }

        if (!read_power_file(base, name, "capacity", str, sizeof (str))) {
            pct = -1;
        } else {
            pct = SDL_atoi(str);
            pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
        }

        if (!read_power_file(base, name, "time_to_empty_now", str, sizeof (str))) {
            secs = -1;
        } else {
            secs = SDL_atoi(str);
            secs = (secs <= 0) ? -1 : secs;  /* 0 == unknown */
        }

        /*
         * We pick the battery that claims to have the most minutes left.
         *  (failing a report of minutes, we'll take the highest percent.)
         */
        if ((secs < 0) && (*seconds < 0)) {
            if ((pct < 0) && (*percent < 0)) {
                choose = SDL_TRUE;  /* at least we know there's a battery. */
            } else if (pct > *percent) {
                choose = SDL_TRUE;
            }
        } else if (secs > *seconds) {
            choose = SDL_TRUE;
        }

        if (choose) {
            *seconds = secs;
            *percent = pct;
            *state = st;
        }
    }

    closedir(dirp);
    return SDL_TRUE;  /* don't look any further. */
}
Example #4
0
static void
check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
                        SDL_bool * charging, int *seconds, int *percent)
{
    const char *base = proc_acpi_battery_path;
    char info[1024];
    char state[1024];
    char *ptr = NULL;
    char *key = NULL;
    char *val = NULL;
    SDL_bool charge = SDL_FALSE;
    SDL_bool choose = SDL_FALSE;
    int maximum = -1;
    int remaining = -1;
    int secs = -1;
    int pct = -1;

    if (!read_power_file(base, node, "state", state, sizeof (state))) {
        return;
    } else if (!read_power_file(base, node, "info", info, sizeof (info))) {
        return;
    }

    ptr = &state[0];
    while (make_proc_acpi_key_val(&ptr, &key, &val)) {
        if (strcmp(key, "present") == 0) {
            if (strcmp(val, "yes") == 0) {
                *have_battery = SDL_TRUE;
            }
        } else if (strcmp(key, "charging state") == 0) {
            /* !!! FIXME: what exactly _does_ charging/discharging mean? */
            if (strcmp(val, "charging/discharging") == 0) {
                charge = SDL_TRUE;
            } else if (strcmp(val, "charging") == 0) {
                charge = SDL_TRUE;
            }
        } else if (strcmp(key, "remaining capacity") == 0) {
            char *endptr = NULL;
            const int cvt = (int) strtol(val, &endptr, 10);
            if (*endptr == ' ') {
                remaining = cvt;
            }
        }
    }

    ptr = &info[0];
    while (make_proc_acpi_key_val(&ptr, &key, &val)) {
        if (strcmp(key, "design capacity") == 0) {
            char *endptr = NULL;
            const int cvt = (int) strtol(val, &endptr, 10);
            if (*endptr == ' ') {
                maximum = cvt;
            }
        }
    }

    if ((maximum >= 0) && (remaining >= 0)) {
        pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f);
        if (pct < 0) {
            pct = 0;
        } else if (pct > 100) {
            pct = 100;
        }
    }

    /* !!! FIXME: calculate (secs). */

    /*
     * We pick the battery that claims to have the most minutes left.
     *  (failing a report of minutes, we'll take the highest percent.)
     */
    if ((secs < 0) && (*seconds < 0)) {
        if ((pct < 0) && (*percent < 0)) {
            choose = SDL_TRUE;  /* at least we know there's a battery. */
        }
        if (pct > *percent) {
            choose = SDL_TRUE;
        }
    } else if (secs > *seconds) {
        choose = SDL_TRUE;
    }

    if (choose) {
        *seconds = secs;
        *percent = pct;
        *charging = charge;
    }
}
Example #5
0
bool PowerX11::GetPowerInfo_Linux_sys_class_power_supply(/*PowerState *state, int *seconds, int *percent*/) {
	const char *base = sys_class_power_supply_path;
	String name;

	DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
	dirp->change_dir(base);
	Error err = dirp->list_dir_begin();

	if (err != OK) {
		return false;
	}

	this->power_state = OS::POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */
	this->nsecs_left = -1;
	this->percent_left = -1;

	name = dirp->get_next();

	while (name != "") {
		bool choose = false;
		char str[64];
		OS::PowerState st;
		int secs;
		int pct;

		if ((name == ".") || (name == "..")) {
			name = dirp->get_next();
			continue; //skip these, of course.
		} else {
			if (!read_power_file(base, name.utf8().get_data(), "type", str, sizeof(str))) {
				name = dirp->get_next();
				continue; // Don't know _what_ we're looking at. Give up on it.
			} else {
				if (String(str) != "Battery\n") {
					name = dirp->get_next();
					continue; // we don't care about UPS and such.
				}
			}
		}

		/* some drivers don't offer this, so if it's not explicitly reported assume it's present. */
		if (read_power_file(base, name.utf8().get_data(), "present", str, sizeof(str)) && (String(str) == "0\n")) {
			st = OS::POWERSTATE_NO_BATTERY;
		} else if (!read_power_file(base, name.utf8().get_data(), "status", str, sizeof(str))) {
			st = OS::POWERSTATE_UNKNOWN; /* uh oh */
		} else if (String(str) == "Charging\n") {
			st = OS::POWERSTATE_CHARGING;
		} else if (String(str) == "Discharging\n") {
			st = OS::POWERSTATE_ON_BATTERY;
		} else if ((String(str) == "Full\n") || (String(str) == "Not charging\n")) {
			st = OS::POWERSTATE_CHARGED;
		} else {
			st = OS::POWERSTATE_UNKNOWN; /* uh oh */
		}

		if (!read_power_file(base, name.utf8().get_data(), "capacity", str, sizeof(str))) {
			pct = -1;
		} else {
			pct = String(str).to_int();
			pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
		}

		if (!read_power_file(base, name.utf8().get_data(), "time_to_empty_now", str, sizeof(str))) {
			secs = -1;
		} else {
			secs = String(str).to_int();
			secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */
		}

		/*
		 * We pick the battery that claims to have the most minutes left.
		 *  (failing a report of minutes, we'll take the highest percent.)
		 */
		if ((secs < 0) && (this->nsecs_left < 0)) {
			if ((pct < 0) && (this->percent_left < 0)) {
				choose = true; /* at least we know there's a battery. */
			} else if (pct > this->percent_left) {
				choose = true;
			}
		} else if (secs > this->nsecs_left) {
			choose = true;
		}

		if (choose) {
			this->nsecs_left = secs;
			this->percent_left = pct;
			this->power_state = st;
		}

		name = dirp->get_next();
	}

	memdelete(dirp);
	return true; /* don't look any further*/
}
Example #6
0
void PowerX11::check_proc_acpi_battery(const char *node, bool *have_battery, bool *charging) {
	const char *base = proc_acpi_battery_path;
	char info[1024];
	char state[1024];
	char *ptr = NULL;
	char *key = NULL;
	char *val = NULL;
	bool charge = false;
	bool choose = false;
	int maximum = -1;
	int remaining = -1;
	int secs = -1;
	int pct = -1;

	if (!read_power_file(base, node, "state", state, sizeof(state))) {
		return;
	} else {
		if (!read_power_file(base, node, "info", info, sizeof(info)))
			return;
	}

	ptr = &state[0];
	while (make_proc_acpi_key_val(&ptr, &key, &val)) {
		if (String(key) == "present") {
			if (String(val) == "yes") {
				*have_battery = true;
			}
		} else if (String(key) == "charging state") {
			/* !!! FIXME: what exactly _does_ charging/discharging mean? */
			if (String(val) == "charging/discharging") {
				charge = true;
			} else if (String(val) == "charging") {
				charge = true;
			}
		} else if (String(key) == "remaining capacity") {
			String sval = val;
			const int cvt = sval.to_int();
			remaining = cvt;
		}
	}

	ptr = &info[0];
	while (make_proc_acpi_key_val(&ptr, &key, &val)) {
		if (String(key) == "design capacity") {
			String sval = val;
			const int cvt = sval.to_int();
			maximum = cvt;
		}
	}

	if ((maximum >= 0) && (remaining >= 0)) {
		pct = (int)((((float)remaining) / ((float)maximum)) * 100.0f);
		if (pct < 0) {
			pct = 0;
		} else if (pct > 100) {
			pct = 100;
		}
	}

	/* !!! FIXME: calculate (secs). */

	/*
	 * We pick the battery that claims to have the most minutes left.
	 *  (failing a report of minutes, we'll take the highest percent.)
	 */
	if ((secs < 0) && (this->nsecs_left < 0)) {
		if ((pct < 0) && (this->percent_left < 0)) {
			choose = true; /* at least we know there's a battery. */
		}
		if (pct > this->percent_left) {
			choose = true;
		}
	} else if (secs > this->nsecs_left) {
		choose = true;
	}

	if (choose) {
		this->nsecs_left = secs;
		this->percent_left = pct;
		*charging = charge;
	}
}