From 60e474ead5db1d8be3ee542caf40fb08650353b1 Mon Sep 17 00:00:00 2001 From: Landry Breuil Date: Wed, 4 Jan 2012 11:27:15 +0100 Subject: 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 --- src/openbsd/up-apm-native.h | 1 + src/openbsd/up-backend.c | 19 ++++--------------- src/openbsd/up-native.c | 25 +++++++++++++++++-------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/openbsd/up-apm-native.h b/src/openbsd/up-apm-native.h index 16ec85e..309edbb 100644 --- a/src/openbsd/up-apm-native.h +++ b/src/openbsd/up-apm-native.h @@ -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 diff --git a/src/openbsd/up-backend.c b/src/openbsd/up-backend.c index d416c74..35ed284 100644 --- a/src/openbsd/up-backend.c +++ b/src/openbsd/up-backend.c @@ -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); } diff --git a/src/openbsd/up-native.c b/src/openbsd/up-native.c index b908c55..020cb6e 100644 --- a/src/openbsd/up-native.c +++ b/src/openbsd/up-native.c @@ -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; } -- cgit v1.2.3