1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 int on_ac_power(void) {
35 struct udev_enumerate *e = NULL;
36 struct udev_list_entry *item = NULL, *first = NULL;
37 bool found_offline = false, found_online = false;
39 if (!(udev = udev_new())) {
44 if (!(e = udev_enumerate_new(udev))) {
49 if (udev_enumerate_add_match_subsystem(e, "power_supply") < 0) {
54 if (udev_enumerate_scan_devices(e) < 0) {
59 first = udev_enumerate_get_list_entry(e);
60 udev_list_entry_foreach(item, first) {
61 struct udev_device *d;
62 const char *type, *online;
64 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
69 if (!(type = udev_device_get_sysattr_value(d, "type")))
72 if (!streq(type, "Mains"))
75 if (!(online = udev_device_get_sysattr_value(d, "online")))
78 if (streq(online, "1")) {
81 } else if (streq(online, "0"))
88 r = found_online || !found_offline;
92 udev_enumerate_unref(e);