chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / login / logind-button.c
index c47c415e6076fa8f17c068c2dbcc9e642957c9b0..0defa6b9ba9f0ac5054e709d87e851acb61dfde3 100644 (file)
@@ -1,21 +1,4 @@
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <errno.h>
 #include <fcntl.h>
@@ -109,9 +92,13 @@ static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
 
         assert(manager);
 
-        /* If we are docked, handle the lid switch differently */
+        /* If we are docked or on external power, handle the lid switch
+         * differently */
         if (manager_is_docked_or_external_displays(manager))
                 handle_action = manager->handle_lid_switch_docked;
+        else if (manager->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
+                 manager_is_on_external_power())
+                handle_action = manager->handle_lid_switch_ep;
         else
                 handle_action = manager->handle_lid_switch;
 
@@ -167,8 +154,7 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                 case KEY_POWER2:
                         log_struct(LOG_INFO,
                                    LOG_MESSAGE("Power key pressed."),
-                                   "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR,
-                                   NULL);
+                                   "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR);
 
                         manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
                         break;
@@ -182,8 +168,7 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                 case KEY_SLEEP:
                         log_struct(LOG_INFO,
                                    LOG_MESSAGE("Suspend key pressed."),
-                                   "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR,
-                                   NULL);
+                                   "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR);
 
                         manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
                         break;
@@ -191,8 +176,7 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                 case KEY_SUSPEND:
                         log_struct(LOG_INFO,
                                    LOG_MESSAGE("Hibernate key pressed."),
-                                   "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR,
-                                   NULL);
+                                   "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR);
 
                         manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
                         break;
@@ -203,8 +187,7 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                 if (ev.code == SW_LID) {
                         log_struct(LOG_INFO,
                                    LOG_MESSAGE("Lid closed."),
-                                   "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR,
-                                   NULL);
+                                   "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR);
 
                         b->lid_closed = true;
                         button_lid_switch_handle_action(b->manager, true);
@@ -213,8 +196,7 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                 } else if (ev.code == SW_DOCK) {
                         log_struct(LOG_INFO,
                                    LOG_MESSAGE("System docked."),
-                                   "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR,
-                                   NULL);
+                                   "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR);
 
                         b->docked = true;
                 }
@@ -224,8 +206,7 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                 if (ev.code == SW_LID) {
                         log_struct(LOG_INFO,
                                    LOG_MESSAGE("Lid opened."),
-                                   "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR,
-                                   NULL);
+                                   "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR);
 
                         b->lid_closed = false;
                         b->check_event_source = sd_event_source_unref(b->check_event_source);
@@ -233,8 +214,7 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                 } else if (ev.code == SW_DOCK) {
                         log_struct(LOG_INFO,
                                    LOG_MESSAGE("System undocked."),
-                                   "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR,
-                                   NULL);
+                                   "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR);
 
                         b->docked = false;
                 }
@@ -243,6 +223,42 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
         return 0;
 }
 
+static int button_suitable(Button *b) {
+        unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
+
+        assert(b);
+        assert(b->fd);
+
+        if (ioctl(b->fd, EVIOCGBIT(EV_SYN, sizeof(types)), types) < 0)
+                return -errno;
+
+        if (bitset_get(types, EV_KEY)) {
+                unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1];
+
+                if (ioctl(b->fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0)
+                        return -errno;
+
+                if (bitset_get(keys, KEY_POWER) ||
+                    bitset_get(keys, KEY_POWER2) ||
+                    bitset_get(keys, KEY_SLEEP) ||
+                    bitset_get(keys, KEY_SUSPEND))
+                        return true;
+        }
+
+        if (bitset_get(types, EV_SW)) {
+                unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
+
+                if (ioctl(b->fd, EVIOCGBIT(EV_SW, sizeof(switches)), switches) < 0)
+                        return -errno;
+
+                if (bitset_get(switches, SW_LID) ||
+                    bitset_get(switches, SW_DOCK))
+                        return true;
+        }
+
+        return false;
+}
+
 static int button_set_mask(Button *b) {
         unsigned long
                 types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
@@ -308,7 +324,15 @@ int button_open(Button *b) {
 
         b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
         if (b->fd < 0)
-                return log_warning_errno(errno, "Failed to open %s: %m", b->name);
+                return log_warning_errno(errno, "Failed to open %s: %m", p);
+
+        r = button_suitable(b);
+        if (r < 0)
+                return log_warning_errno(r, "Failed to determine whether input device is relevant to us: %m");
+        if (r == 0) {
+                log_debug("Device %s does not expose keys or switches relevant to us, ignoring.", p);
+                return -EADDRNOTAVAIL;
+        }
 
         if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
                 r = log_error_errno(errno, "Failed to get input name: %m");