OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <glib.h> | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/platform_thread.h" | |
9 #include "power_manager/udev_listener.h" | |
tfarina
2011/04/14 02:36:05
this should be included first. Please move it to l
marcheu
2011/04/14 02:52:10
Why? The convention is to order the headers alphab
tfarina
2011/04/14 14:53:58
See http://google-styleguide.googlecode.com/svn/tr
| |
10 | |
11 namespace power_manager { | |
12 | |
13 UdevListener::UdevListener(UdevCallback* callback, std::string subsystem) | |
14 : callback_(callback), | |
15 udev_(NULL), | |
16 subsystem_(subsystem) { | |
17 } | |
18 | |
19 UdevListener::~UdevListener() { | |
20 if (udev_) | |
21 udev_unref(udev_); | |
22 } | |
23 | |
24 bool UdevListener::Init() { | |
25 // Create the udev object. | |
26 udev_ = udev_new(); | |
27 if (!udev_) { | |
28 LOG(ERROR) << "Can't create udev object."; | |
29 return false; | |
30 } | |
31 | |
32 // Create the udev monitor structure. | |
33 monitor_ = udev_monitor_new_from_netlink(udev_, "udev"); | |
34 if (!monitor_ ) { | |
35 LOG(ERROR) << "Can't create udev monitor."; | |
36 udev_unref(udev_); | |
37 return false; | |
38 } | |
39 udev_monitor_filter_add_match_subsystem_devtype(monitor_, | |
40 subsystem_.c_str(), | |
41 NULL); | |
42 udev_monitor_enable_receiving(monitor_); | |
43 | |
44 int fd = udev_monitor_get_fd(monitor_); | |
45 | |
46 GIOChannel* channel = g_io_channel_unix_new(fd); | |
47 g_io_add_watch(channel, G_IO_IN, &(UdevListener::EventHandler), this); | |
48 | |
49 LOG(INFO) << "Udev listener waiting for events on subsystem " | |
50 << subsystem_; | |
51 | |
52 return true; | |
53 } | |
54 | |
55 gboolean UdevListener::EventHandler(GIOChannel* source, | |
56 GIOCondition condition, | |
57 gpointer data) { | |
58 UdevListener* listener = static_cast<UdevListener*>(data); | |
59 | |
60 struct udev_device* dev = udev_monitor_receive_device(listener->monitor_); | |
61 if (dev) { | |
62 LOG(INFO) << "Event on (" | |
63 << udev_device_get_devnode(dev) | |
64 << "|" | |
65 << udev_device_get_subsystem(dev) | |
66 << "|" | |
67 << udev_device_get_devtype(dev) | |
68 << ") Action " | |
69 << udev_device_get_action(dev); | |
70 udev_device_unref(dev); | |
71 listener->callback_->Run(source, condition); | |
72 } else { | |
73 LOG(ERROR) << "Can't get receive_device()"; | |
74 return false; | |
75 } | |
76 return true; | |
77 } | |
78 | |
79 } // namespace power_manager | |
80 | |
tfarina
2011/04/14 02:36:05
remove this blank line.
marcheu
2011/04/14 02:52:10
Done.
| |
OLD | NEW |