mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2026-04-22 16:20:43 +02:00
openbsd: use a singleton pattern to access /dev/apm
up_apm_get_fd() opens /dev/apm only if it's not already opened. Signed-off-by: Richard Hughes <richard@hughsie.com>
This commit is contained in:
parent
6c15694dbb
commit
60e474ead5
3 changed files with 22 additions and 23 deletions
|
|
@ -71,6 +71,7 @@ typedef struct
|
|||
|
||||
UpApmNative* up_apm_native_new (const char*);
|
||||
const gchar * up_apm_native_get_path(UpApmNative*);
|
||||
int up_apm_get_fd();
|
||||
gboolean up_native_is_laptop();
|
||||
gboolean up_native_get_sensordev(const char*, struct sensordev*);
|
||||
G_END_DECLS
|
||||
|
|
|
|||
|
|
@ -62,8 +62,6 @@ enum {
|
|||
|
||||
static guint signals [SIGNAL_LAST] = { 0 };
|
||||
|
||||
int apm_fd = 0; /* ugly global.. needs to move to a device native object */
|
||||
|
||||
G_DEFINE_TYPE (UpBackend, up_backend, G_TYPE_OBJECT)
|
||||
|
||||
/**
|
||||
|
|
@ -279,7 +277,7 @@ up_backend_update_ac_state(UpDevice* device)
|
|||
gboolean ret, new_is_online, cur_is_online;
|
||||
struct apm_power_info a;
|
||||
|
||||
ret = up_backend_apm_get_power_info(apm_fd, &a);
|
||||
ret = up_backend_apm_get_power_info(up_apm_get_fd(), &a);
|
||||
if (!ret)
|
||||
return ret;
|
||||
|
||||
|
|
@ -306,7 +304,7 @@ up_backend_update_battery_state(UpDevice* device)
|
|||
gint64 cur_time_to_empty, new_time_to_empty;
|
||||
struct apm_power_info a;
|
||||
|
||||
ret = up_backend_apm_get_power_info(apm_fd, &a);
|
||||
ret = up_backend_apm_get_power_info(up_apm_get_fd(), &a);
|
||||
if (!ret)
|
||||
return ret;
|
||||
|
||||
|
|
@ -423,10 +421,6 @@ up_apm_device_refresh(UpDevice* device)
|
|||
UpDeviceKind type;
|
||||
GTimeVal timeval;
|
||||
gboolean ret;
|
||||
if (apm_fd == 0) {
|
||||
g_debug("refresh callback called but apm_fd is not initialized yet");
|
||||
return TRUE;
|
||||
}
|
||||
g_object_get (device, "type", &type, NULL);
|
||||
|
||||
switch (type) {
|
||||
|
|
@ -464,15 +458,10 @@ up_backend_apm_event_thread(gpointer object)
|
|||
|
||||
g_debug("setting up apm thread");
|
||||
|
||||
/* open /dev/apm */
|
||||
if ((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
|
||||
if (errno != ENXIO && errno != ENOENT)
|
||||
g_error("cannot open device file");
|
||||
}
|
||||
kq = kqueue();
|
||||
if (kq <= 0)
|
||||
g_error("kqueue");
|
||||
EV_SET(&ev, apm_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,
|
||||
EV_SET(&ev, up_apm_get_fd(), EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,
|
||||
0, 0, NULL);
|
||||
nevents = 1;
|
||||
if (kevent(kq, &ev, nevents, NULL, 0, &sts) < 0)
|
||||
|
|
@ -488,7 +477,7 @@ up_backend_apm_event_thread(gpointer object)
|
|||
break;
|
||||
if (!rv)
|
||||
continue;
|
||||
if (ev.ident == (guint) apm_fd && APM_EVENT_TYPE(ev.data) == APM_POWER_CHANGE ) {
|
||||
if (ev.ident == (guint) up_apm_get_fd() && APM_EVENT_TYPE(ev.data) == APM_POWER_CHANGE ) {
|
||||
/* g_idle_add the callback */
|
||||
g_idle_add((GSourceFunc) up_backend_apm_powerchange_event_cb, backend);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,21 @@ up_apm_native_get_path(UpApmNative * native)
|
|||
return native->path;
|
||||
}
|
||||
|
||||
int
|
||||
up_apm_get_fd()
|
||||
{
|
||||
static int apm_fd = 0;
|
||||
if (apm_fd == 0) {
|
||||
g_debug("apm_fd is not initialized yet, opening");
|
||||
/* open /dev/apm */
|
||||
if ((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
|
||||
if (errno != ENXIO && errno != ENOENT)
|
||||
g_error("cannot open device file");
|
||||
}
|
||||
}
|
||||
return apm_fd;
|
||||
}
|
||||
|
||||
/**
|
||||
* up_native_get_native_path:
|
||||
* @object: the native tracking object
|
||||
|
|
@ -74,20 +89,14 @@ up_native_get_native_path (GObject *object)
|
|||
gboolean
|
||||
up_native_is_laptop()
|
||||
{
|
||||
int apm_fd;
|
||||
struct apm_power_info bstate;
|
||||
struct sensordev acpiac;
|
||||
|
||||
if (up_native_get_sensordev("acpiac0", &acpiac))
|
||||
return TRUE;
|
||||
|
||||
if ((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
|
||||
if (errno != ENXIO && errno != ENOENT)
|
||||
g_error("cannot open device file");
|
||||
}
|
||||
if (-1 == ioctl(apm_fd, APM_IOC_GETPOWER, &bstate))
|
||||
g_error("ioctl on fd %d failed : %s", apm_fd, g_strerror(errno));
|
||||
close(apm_fd);
|
||||
if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate))
|
||||
g_error("ioctl on apm fd failed : %s", g_strerror(errno));
|
||||
return bstate.ac_state != APM_AC_UNKNOWN;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue