xref: /haiku/src/servers/power/power_button_monitor.cpp (revision bf29cdd77788f6761ca85f6039abec2589c45194)
1 /*
2  * Copyright 2005-2014, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Rene Gollent
7  *		Nathan Whitehorn
8  */
9 
10 
11 #include "power_button_monitor.h"
12 
13 #include <string.h>
14 
15 #include <Directory.h>
16 #include <Messenger.h>
17 #include <Roster.h>
18 #include <String.h>
19 
20 #include <RosterPrivate.h>
21 
22 
23 static const char* kBasePath = "/dev/power/button";
24 
25 
PowerButtonMonitor()26 PowerButtonMonitor::PowerButtonMonitor()
27 	:
28 	fFDs()
29 {
30 	BDirectory dir;
31 	if (dir.SetTo(kBasePath) != B_OK)
32 		return;
33 
34 	entry_ref ref;
35 	while (dir.GetNextRef(&ref) == B_OK) {
36 		if (strncmp(ref.name, "power", 5) == 0) {
37 			BString path;
38 			path.SetToFormat("%s/%s", kBasePath, ref.name);
39 			int fd = open(path.String(), O_RDONLY);
40 			if (fd > 0)
41 				fFDs.insert(fd);
42 		}
43 	}
44 }
45 
46 
~PowerButtonMonitor()47 PowerButtonMonitor::~PowerButtonMonitor()
48 {
49 	for (std::set<int>::iterator it = fFDs.begin(); it != fFDs.end(); ++it)
50 		close(*it);
51 }
52 
53 
54 void
HandleEvent(int fd)55 PowerButtonMonitor::HandleEvent(int fd)
56 {
57 	uint8 button_pressed;
58 	if (read(fd, &button_pressed, 1) != 1)
59 		return;
60 
61 	if (button_pressed) {
62 		BRoster roster;
63 		BRoster::Private rosterPrivate(roster);
64 
65 		rosterPrivate.ShutDown(false, false, false);
66 	}
67 }
68