power_daemon: checks the device descriptors are valid
* acpi_lid is in fact optionally available.
This commit is contained in:
committed by
Jérôme Duval
parent
cb7df3b1da
commit
5641f2eae3
@@ -9,14 +9,17 @@
|
||||
#define _LID_MONITOR_H
|
||||
|
||||
|
||||
class LidMonitor {
|
||||
#include "power_monitor.h"
|
||||
|
||||
|
||||
class LidMonitor : public PowerMonitor {
|
||||
public:
|
||||
LidMonitor();
|
||||
~LidMonitor();
|
||||
virtual ~LidMonitor();
|
||||
|
||||
void HandleEvent();
|
||||
virtual void HandleEvent();
|
||||
|
||||
int FD() const { return fFD; }
|
||||
virtual int FD() const { return fFD; }
|
||||
private:
|
||||
int fFD;
|
||||
};
|
||||
|
||||
@@ -9,14 +9,17 @@
|
||||
#define _POWER_BUTTON_MONITOR_H
|
||||
|
||||
|
||||
class PowerButtonMonitor {
|
||||
#include "power_monitor.h"
|
||||
|
||||
|
||||
class PowerButtonMonitor : public PowerMonitor {
|
||||
public:
|
||||
PowerButtonMonitor();
|
||||
~PowerButtonMonitor();
|
||||
virtual ~PowerButtonMonitor();
|
||||
|
||||
void HandleEvent();
|
||||
virtual void HandleEvent();
|
||||
|
||||
int FD() const { return fFD; }
|
||||
virtual int FD() const { return fFD; }
|
||||
private:
|
||||
int fFD;
|
||||
};
|
||||
|
||||
@@ -21,8 +21,8 @@ private:
|
||||
static status_t _EventLooper(void *arg);
|
||||
|
||||
thread_id fEventThread;
|
||||
PowerButtonMonitor* fPowerButtonMonitor;
|
||||
LidMonitor* fLidMonitor;
|
||||
PowerMonitor* fPowerMonitors[2];
|
||||
uint32 fMonitorCount;
|
||||
|
||||
bool fQuitRequested;
|
||||
};
|
||||
@@ -41,11 +41,21 @@ main(void)
|
||||
PowerManagementDaemon::PowerManagementDaemon()
|
||||
:
|
||||
BApplication("application/x-vnd.Haiku-powermanagement"),
|
||||
fPowerButtonMonitor(NULL),
|
||||
fMonitorCount(0),
|
||||
fQuitRequested(false)
|
||||
{
|
||||
fPowerButtonMonitor = new PowerButtonMonitor;
|
||||
fLidMonitor = new LidMonitor;
|
||||
PowerMonitor* powerButtonMonitor = new PowerButtonMonitor;
|
||||
if (powerButtonMonitor->FD() > 0)
|
||||
fPowerMonitors[fMonitorCount++] = powerButtonMonitor;
|
||||
else
|
||||
delete powerButtonMonitor;
|
||||
|
||||
PowerMonitor* lidMonitor = new LidMonitor;
|
||||
if (lidMonitor->FD() > 0)
|
||||
fPowerMonitors[fMonitorCount++] = lidMonitor;
|
||||
else
|
||||
delete lidMonitor;
|
||||
|
||||
fEventThread = spawn_thread(_EventLooper, "_power_daemon_event_loop_",
|
||||
B_NORMAL_PRIORITY, this);
|
||||
if (fEventThread < B_OK)
|
||||
@@ -61,8 +71,8 @@ PowerManagementDaemon::PowerManagementDaemon()
|
||||
PowerManagementDaemon::~PowerManagementDaemon()
|
||||
{
|
||||
fQuitRequested = true;
|
||||
delete fPowerButtonMonitor;
|
||||
delete fLidMonitor;
|
||||
for (uint32 i = 0; i < fMonitorCount; i++)
|
||||
delete fPowerMonitors[i];
|
||||
status_t status;
|
||||
wait_for_thread(fEventThread, &status);
|
||||
}
|
||||
@@ -80,17 +90,23 @@ PowerManagementDaemon::_EventLooper(void* arg)
|
||||
void
|
||||
PowerManagementDaemon::_EventLoop()
|
||||
{
|
||||
if (fMonitorCount == 0)
|
||||
return;
|
||||
object_wait_info info[fMonitorCount];
|
||||
for (uint32 i = 0; i < fMonitorCount; i++) {
|
||||
info[i].object = fPowerMonitors[i]->FD();
|
||||
info[i].type = B_OBJECT_TYPE_FD;
|
||||
info[i].events = B_EVENT_READ;
|
||||
}
|
||||
while (!fQuitRequested) {
|
||||
object_wait_info info[] = {
|
||||
{ fPowerButtonMonitor->FD(), B_OBJECT_TYPE_FD, B_EVENT_READ },
|
||||
{ fLidMonitor->FD(), B_OBJECT_TYPE_FD, B_EVENT_READ }
|
||||
};
|
||||
|
||||
if (wait_for_objects_etc(info, 2, 0, 1000000LL) < B_OK)
|
||||
if (wait_for_objects(info, fMonitorCount) < B_OK)
|
||||
continue;
|
||||
if (info[0].events & B_EVENT_READ)
|
||||
fPowerButtonMonitor->HandleEvent();
|
||||
if (info[1].events & B_EVENT_READ)
|
||||
fLidMonitor->HandleEvent();
|
||||
// handle events and reset events
|
||||
for (uint32 i = 0; i < fMonitorCount; i++) {
|
||||
if (info[i].events & B_EVENT_READ)
|
||||
fPowerMonitors[i]->HandleEvent();
|
||||
else
|
||||
info[i].events = B_EVENT_READ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2013, Haiku, Inc.
|
||||
* Distributed under the terms of the MIT license.
|
||||
*
|
||||
* Authors:
|
||||
* Jérôme Duval, [email protected].
|
||||
*/
|
||||
#ifndef _POWER_MONITOR_H
|
||||
#define _POWER_MONITOR_H
|
||||
|
||||
|
||||
class PowerMonitor {
|
||||
public:
|
||||
virtual ~PowerMonitor() {};
|
||||
|
||||
virtual void HandleEvent() = 0;
|
||||
|
||||
virtual int FD() const = 0;
|
||||
};
|
||||
|
||||
|
||||
#endif // _POWER_MONITOR_H
|
||||
Reference in New Issue
Block a user