2 * Copyright 2013, Jérôme Duval, korli@users.berlios.de.
3 * Copyright 2014, Rene Gollent, rene@gollent.com.
4 * Copyright 2005, Nathan Whitehorn.
6 * Distributed under the terms of the MIT License.
10 #include "lid_monitor.h"
11 #include "power_button_monitor.h"
13 #include <Application.h>
18 class PowerManagementDaemon
: public BApplication
{
20 PowerManagementDaemon();
21 virtual ~PowerManagementDaemon();
24 static status_t
_EventLooper(void *arg
);
26 thread_id fEventThread
;
27 PowerMonitor
* fPowerMonitors
[2];
37 new PowerManagementDaemon();
44 PowerManagementDaemon::PowerManagementDaemon()
46 BApplication("application/x-vnd.Haiku-powermanagement"),
50 PowerMonitor
* powerButtonMonitor
= new PowerButtonMonitor
;
51 if (powerButtonMonitor
->FDs().size() > 0)
52 fPowerMonitors
[fMonitorCount
++] = powerButtonMonitor
;
54 delete powerButtonMonitor
;
56 PowerMonitor
* lidMonitor
= new LidMonitor
;
57 if (lidMonitor
->FDs().size() > 0)
58 fPowerMonitors
[fMonitorCount
++] = lidMonitor
;
62 fEventThread
= spawn_thread(_EventLooper
, "_power_daemon_event_loop_",
63 B_NORMAL_PRIORITY
, this);
64 if (fEventThread
< B_OK
)
66 if (resume_thread(fEventThread
) < B_OK
) {
67 kill_thread(fEventThread
);
74 PowerManagementDaemon::~PowerManagementDaemon()
76 fQuitRequested
= true;
77 for (uint32 i
= 0; i
< fMonitorCount
; i
++)
78 delete fPowerMonitors
[i
];
80 wait_for_thread(fEventThread
, &status
);
85 PowerManagementDaemon::_EventLooper(void* arg
)
87 PowerManagementDaemon
* self
= (PowerManagementDaemon
*)arg
;
94 PowerManagementDaemon::_EventLoop()
96 if (fMonitorCount
== 0)
99 std::map
<int, PowerMonitor
*> descriptorMap
;
102 for (uint32 i
= 0; i
< fMonitorCount
; i
++)
103 fdCount
+= fPowerMonitors
[i
]->FDs().size();
105 object_wait_info info
[fdCount
];
107 for (uint32 i
= 0; i
< fMonitorCount
; i
++) {
108 const std::set
<int>& fds
= fPowerMonitors
[i
]->FDs();
109 for (std::set
<int>::iterator it
= fds
.begin(); it
!= fds
.end(); ++it
) {
110 info
[index
].object
= *it
;
111 info
[index
].type
= B_OBJECT_TYPE_FD
;
112 info
[index
].events
= B_EVENT_READ
;
113 descriptorMap
[*it
] = fPowerMonitors
[i
];
117 while (!fQuitRequested
) {
118 if (wait_for_objects(info
, fdCount
) < B_OK
)
120 // handle events and reset events
121 for (uint32 i
= 0; i
< fdCount
; i
++) {
122 if (info
[i
].events
& B_EVENT_READ
)
123 descriptorMap
[info
[i
].object
]->HandleEvent(info
[i
].object
);
125 info
[i
].events
= B_EVENT_READ
;