logind-button.c revision 6524990fdc98370ecba5d9f73e67161e8798c010
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
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/>.
***/
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "conf-parser.h"
#include "util.h"
#include "logind-button.h"
#include "special.h"
#include "dbus-common.h"
Button *b;
assert(m);
if (!b)
return NULL;
if (!b->name) {
free(b);
return NULL;
}
free(b);
return NULL;
}
b->manager = m;
b->fd = -1;
return b;
}
void button_free(Button *b) {
assert(b);
if (b->fd >= 0) {
close_nointr_nofail(b->fd);
}
free(b);
}
char *s;
assert(b);
if (!s)
return -ENOMEM;
b->seat = s;
return 0;
}
int button_open(Button *b) {
char name[256], *p;
struct epoll_event ev;
int r;
assert(b);
if (b->fd >= 0) {
close_nointr_nofail(b->fd);
b->fd = -1;
}
if (!p)
return log_oom();
free(p);
if (b->fd < 0) {
return -errno;
}
log_error("Failed to get input name: %m");
r = -errno;
goto fail;
}
log_error("Failed to add to epoll: %m");
r = -errno;
goto fail;
}
if (r < 0) {
goto fail;
}
return 0;
fail:
close_nointr_nofail(b->fd);
b->fd = -1;
return r;
}
static int button_handle(
Button *b,
bool ignore_inhibited,
bool is_edge) {
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...",
[HANDLE_HYBRID_SLEEP] = "Hibernating and suspend...",
};
static const char * const target_table[_HANDLE_BUTTON_MAX] = {
};
int r;
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 */
return 0;
}
inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
/* If the actual operation is inhibited, warn and fail */
if (!ignore_inhibited &&
/* If this is just a recheck of the lid switch then don't warn about anything */
if (!is_edge) {
return 0;
}
warn_melody();
return -EPERM;
}
/* We are executing the operation, so make sure we don't
b->lid_close_queued = false;
r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, target_table[handle], inhibit_operation, &error);
if (r < 0) {
return r;
}
return 1;
}
int button_process(Button *b) {
struct input_event ev;
ssize_t l;
assert(b);
if (l < 0)
return -EIO;
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, 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:
log_info("Suspend key pressed.");
return button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
case KEY_SUSPEND:
log_info("Hibernate key pressed.");
return button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
}
case SW_LID:
log_info("Lid closed.");
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);
}
case SW_LID:
log_info("Lid opened.");
b->lid_close_queued = false;
break;
}
}
return 0;
}
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);
}
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",
[HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
};
DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_button, handle_button, HandleButton, "Failed to parse handle button setting");