chiark / gitweb /
logind: add HandleLidSwitchDocked= option to logind.conf + documentation
[elogind.git] / src / login / logind-button.c
index 720071a2a846faeb8cd9b0b9e51ba0c32bdc8a62..57e619efe690b801e80fa65cb5115afcb58a48ae 100644 (file)
@@ -72,7 +72,7 @@ void button_free(Button *b) {
         if (b->fd >= 0) {
                 /* If the device has been unplugged close() returns
                  * ENODEV, let's ignore this, hence we don't use
-                 * close_nointr_nofail() */
+                 * safe_close() */
                 close(b->fd);
         }
 
@@ -97,13 +97,27 @@ int button_set_seat(Button *b, const char *sn) {
         return 0;
 }
 
+static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
+        HandleAction handle_action;
+
+        assert(manager);
+
+        /* If we are docked, handle the lid switch differently */
+        if (manager_is_docked_or_multiple_displays(manager))
+                handle_action = manager->handle_lid_switch_docked;
+        else
+                handle_action = manager->handle_lid_switch;
+
+        manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
+}
+
 static int button_recheck(sd_event_source *e, void *userdata) {
         Button *b = userdata;
 
         assert(b);
         assert(b->lid_closed);
 
-        manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);
+        button_lid_switch_handle_action(b->manager, false);
         return 1;
 }
 
@@ -186,8 +200,16 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
                                    NULL);
 
                         b->lid_closed = true;
-                        manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
+                        button_lid_switch_handle_action(b->manager, true);
                         button_install_check_event_source(b);
+
+                } else if (ev.code == SW_DOCK) {
+                        log_struct(LOG_INFO,
+                                   "MESSAGE=System docked.",
+                                   MESSAGE_ID(SD_MESSAGE_SYSTEM_DOCKED),
+                                   NULL);
+
+                        b->docked = true;
                 }
 
         } else if (ev.type == EV_SW && ev.value == 0) {
@@ -200,6 +222,14 @@ static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *u
 
                         b->lid_closed = false;
                         b->check_event_source = sd_event_source_unref(b->check_event_source);
+
+                } else if (ev.code == SW_DOCK) {
+                        log_struct(LOG_INFO,
+                                   "MESSAGE=System undocked.",
+                                   MESSAGE_ID(SD_MESSAGE_SYSTEM_UNDOCKED),
+                                   NULL);
+
+                        b->docked = false;
                 }
         }
 
@@ -247,7 +277,7 @@ fail:
         return r;
 }
 
-int button_check_lid(Button *b) {
+int button_check_switches(Button *b) {
         uint8_t switches[SW_MAX/8+1] = {};
         assert(b);
 
@@ -258,11 +288,10 @@ int button_check_lid(Button *b) {
                 return -errno;
 
         b->lid_closed = (switches[SW_LID/8] >> (SW_LID % 8)) & 1;
+        b->docked = (switches[SW_DOCK/8] >> (SW_DOCK % 8)) & 1;
 
-        if (b->lid_closed) {
-                manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
+        if (b->lid_closed)
                 button_install_check_event_source(b);
-        }
 
         return 0;
 }