X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flogin%2Flogind-button.c;h=ea45c28eefe8fad705fb50ce24b9eef6b1301112;hb=d1148ed10a474ccc949113a8ec06e7e29c4c7cb0;hp=7cb3f383bcb2c926954f81d7e582d29ac0583a30;hpb=beaafb2ea6be591882aef21fe19b88e3b2461087;p=elogind.git diff --git a/src/login/logind-button.c b/src/login/logind-button.c index 7cb3f383b..ea45c28ee 100644 --- a/src/login/logind-button.c +++ b/src/login/logind-button.c @@ -33,6 +33,7 @@ #include "logind-button.h" #include "special.h" #include "dbus-common.h" +#include "sd-messages.h" Button* button_new(Manager *m, const char *name) { Button *b; @@ -70,7 +71,11 @@ void button_free(Button *b) { if (b->fd >= 0) { hashmap_remove(b->manager->button_fds, INT_TO_PTR(b->fd + 1)); assert_se(epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_DEL, b->fd, NULL) == 0); - close_nointr_nofail(b->fd); + + /* If the device has been unplugged close() returns + * ENODEV, let's ignore this, hence we don't use + * close_nointr_nofail() */ + close(b->fd); } free(b->name); @@ -102,7 +107,7 @@ int button_open(Button *b) { assert(b); if (b->fd >= 0) { - close_nointr_nofail(b->fd); + close(b->fd); b->fd = -1; } @@ -145,67 +150,27 @@ int button_open(Button *b) { return 0; fail: - close_nointr_nofail(b->fd); + close(b->fd); b->fd = -1; return r; } -static int button_handle(Button *b, InhibitWhat inhibit_key, HandleButton handle, bool ignore_inhibited) { - - static const char * const message_table[_HANDLE_BUTTON_MAX] = { - [HANDLE_POWEROFF] = "Powering Off...", - [HANDLE_REBOOT] = "Rebooting...", - [HANDLE_HALT] = "Halting...", - [HANDLE_KEXEC] = "Rebooting via kexec...", - [HANDLE_SUSPEND] = "Suspending...", - [HANDLE_HIBERNATE] = "Hibernating..." - }; - - static const char * const target_table[_HANDLE_BUTTON_MAX] = { - [HANDLE_POWEROFF] = "poweroff.target", - [HANDLE_REBOOT] = "reboot.target", - [HANDLE_HALT] = "halt.target", - [HANDLE_KEXEC] = "kexec.target", - [HANDLE_SUSPEND] = "suspend.target", - [HANDLE_HIBERNATE] = "hibernate.target" - }; - - DBusError error; +static int button_handle( + Button *b, + InhibitWhat inhibit_key, + HandleAction handle, + bool ignore_inhibited, + bool is_edge) { + int r; - InhibitWhat inhibit_operation; assert(b); - /* If the key handling is turned off, don't do anything */ - if (handle == HANDLE_IGNORE) { - log_debug("Refusing key handling, as it is turned off."); - return 0; - } - - /* If the key handling is inhibited, don't do anything */ - if (manager_is_inhibited(b->manager, inhibit_key, INHIBIT_BLOCK, NULL, true)) { - log_debug("Refusing key handling, %s is inhibited.", inhibit_what_to_string(inhibit_key)); - return 0; - } - - inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN; - - /* If the actual operation is inhibited, warn and fail */ - if (!ignore_inhibited && - manager_is_inhibited(b->manager, inhibit_operation, INHIBIT_BLOCK, NULL, false)) { - log_error("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation)); - warn_melody(); - return -EPERM; - } - - log_info("%s", message_table[handle]); - - dbus_error_init(&error); - r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, target_table[handle], inhibit_operation, &error); - if (r < 0) { - log_error("Failed to execute operation: %s", bus_error_message(&error)); - dbus_error_free(&error); - } + r = manager_handle_action(b->manager, inhibit_key, handle, ignore_inhibited, is_edge); + if (r > 0) + /* We are executing the operation, so make sure we don't + * execute another one until the lid is opened/closed again */ + b->lid_close_queued = false; return r; } @@ -228,36 +193,69 @@ int button_process(Button *b) { case KEY_POWER: case KEY_POWER2: - log_info("Power key pressed."); - return button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited); + log_struct(LOG_INFO, + "MESSAGE=Power key pressed.", + MESSAGE_ID(SD_MESSAGE_POWER_KEY), + NULL); + return button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true); + + /* The kernel is a bit confused here: + + KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend" + KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate" + */ case KEY_SLEEP: - case KEY_SUSPEND: - log_info("Sleep key pressed."); - return button_handle(b, INHIBIT_HANDLE_SLEEP_KEY, b->manager->handle_sleep_key, b->manager->sleep_key_ignore_inhibited); + log_struct(LOG_INFO, + "MESSAGE=Suspend key pressed.", + MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY), + NULL); + return button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true); + case KEY_SUSPEND: + log_struct(LOG_INFO, + "MESSAGE=Hibernate key pressed.", + MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY), + NULL); + return button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true); } + } else if (ev.type == EV_SW && ev.value > 0) { switch (ev.code) { case SW_LID: - log_info("Lid closed."); - return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited); + log_struct(LOG_INFO, + "MESSAGE=Lid closed.", + MESSAGE_ID(SD_MESSAGE_LID_CLOSED), + NULL); + b->lid_close_queued = true; + + return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true); + } + + } else if (ev.type == EV_SW && ev.value == 0) { + + switch (ev.code) { + + case SW_LID: + log_struct(LOG_INFO, + "MESSAGE=Lid opened.", + MESSAGE_ID(SD_MESSAGE_LID_OPENED), + NULL); + b->lid_close_queued = false; + break; } } return 0; } -static const char* const handle_button_table[_HANDLE_BUTTON_MAX] = { - [HANDLE_IGNORE] = "ignore", - [HANDLE_POWEROFF] = "poweroff", - [HANDLE_REBOOT] = "reboot", - [HANDLE_HALT] = "halt", - [HANDLE_KEXEC] = "kexec", - [HANDLE_SUSPEND] = "suspend", - [HANDLE_HIBERNATE] = "hibernate" -}; -DEFINE_STRING_TABLE_LOOKUP(handle_button, HandleButton); -DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_button, handle_button, HandleButton, "Failed to parse handle button setting"); +int button_recheck(Button *b) { + assert(b); + + if (!b->lid_close_queued) + return 0; + + return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false); +}