xref: /haiku/src/servers/power/power_button_monitor.cpp (revision 28fd553722599b20aed5fdf2d704b6a30ea126c9)
1 /*
2  * Copyright 2005-2013, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Nathan Whitehorn
7  */
8 
9 
10 #include "power_button_monitor.h"
11 
12 #include <Messenger.h>
13 #include <Roster.h>
14 
15 #include <RosterPrivate.h>
16 
17 
18 PowerButtonMonitor::PowerButtonMonitor()
19 	:
20 	BHandler ("power_button_monitor")
21 {
22 	fPowerButtonFD = open("/dev/power/button/power", O_RDONLY);
23 }
24 
25 
26 PowerButtonMonitor::~PowerButtonMonitor()
27 {
28 	if (fPowerButtonFD > 0)
29 		close(fPowerButtonFD);
30 }
31 
32 
33 void
34 PowerButtonMonitor::MessageReceived(BMessage *msg)
35 {
36 	if (msg->what != POLL_POWER_BUTTON_STATUS)
37 		return;
38 
39 	if (fPowerButtonFD <= 0)
40 		return;
41 
42 	uint8 button_pressed;
43 	read(fPowerButtonFD, &button_pressed, 1);
44 
45 	if (button_pressed) {
46 		BRoster roster;
47 		BRoster::Private rosterPrivate(roster);
48 
49 		rosterPrivate.ShutDown(false, false, false);
50 	}
51 }
52