logind-button.c revision 7b77ed8cf36e8eca6017791626044b61ae2d68e7
/*-*- 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 "sd-messages.h"
#include "conf-parser.h"
#include "util.h"
#include "special.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
* close_nointr_nofail() */
}
free(b);
}
char *s;
assert(b);
if (!s)
return -ENOMEM;
b->seat = s;
return 0;
}
static int button_handle(
Button *b,
bool ignore_inhibited,
bool is_edge) {
int r;
assert(b);
if (r > 0)
/* We are executing the operation, so make sure we don't
b->lid_close_queued = false;
return 0;
}
struct input_event ev;
ssize_t l;
assert(s);
assert(b);
if (l < 0)
return -EIO;
case KEY_POWER:
case KEY_POWER2:
"MESSAGE=Power key pressed.",
NULL);
button_handle(b, 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:
"MESSAGE=Suspend key pressed.",
NULL);
button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
break;
case KEY_SUSPEND:
"MESSAGE=Hibernate key pressed.",
NULL);
button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
break;
}
"MESSAGE=Lid closed.",
NULL);
b->lid_close_queued = true;
button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
}
"MESSAGE=Lid opened.",
NULL);
b->lid_close_queued = false;
}
}
return 0;
}
int button_open(Button *b) {
char *p, name[256];
int r;
assert(b);
if (b->fd >= 0) {
b->fd = -1;
}
if (b->fd < 0) {
return -errno;
}
log_error("Failed to get input name: %m");
r = -errno;
goto fail;
}
if (r < 0) {
goto fail;
}
return 0;
fail:
b->fd = -1;
return r;
}
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);
}