logind-button.c revision 7f6e12b03300ba3e473ce6b85d823fc0375b335e
/*-*- 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 <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "sd-messages.h"
#include "util.h"
#include "logind-button.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) {
/* If the device has been unplugged close() returns
* ENODEV, let's ignore this, hence we don't use
* safe_close() */
}
free(b);
}
char *s;
assert(b);
if (!s)
return -ENOMEM;
b->seat = s;
return 0;
}
/* If we are docked, handle the lid switch differently */
else
manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
}
assert(b);
assert(b->lid_closed);
button_lid_switch_handle_action(b->manager, false);
return 1;
}
static int button_install_check_event_source(Button *b) {
int r;
assert(b);
/* Install a post handler, so that we keep rechecking as long as the lid is closed. */
if (b->check_event_source)
return 0;
if (r < 0)
return r;
}
struct input_event ev;
ssize_t l;
assert(s);
assert(b);
if (l < 0)
return -EIO;
case KEY_POWER:
case KEY_POWER2:
LOG_MESSAGE("Power key pressed."),
NULL);
manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
break;
/* 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_MESSAGE("Suspend key pressed."),
NULL);
manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
break;
case KEY_SUSPEND:
LOG_MESSAGE("Hibernate key pressed."),
NULL);
manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
break;
}
LOG_MESSAGE("Lid closed."),
NULL);
b->lid_closed = true;
button_lid_switch_handle_action(b->manager, true);
LOG_MESSAGE("System docked."),
NULL);
b->docked = true;
}
LOG_MESSAGE("Lid opened."),
NULL);
b->lid_closed = false;
LOG_MESSAGE("System undocked."),
NULL);
b->docked = false;
}
}
return 0;
}
int button_open(Button *b) {
char *p, name[256];
int r;
assert(b);
if (b->fd < 0)
r = -errno;
goto fail;
}
if (r < 0) {
log_error_errno(r, "Failed to add button event: %m");
goto fail;
}
return 0;
fail:
return r;
}
int button_check_switches(Button *b) {
assert(b);
if (b->fd < 0)
return -EINVAL;
return -errno;
if (b->lid_closed)
return 0;
}