Add DPMS and lock handling

This commit is contained in:
Mattias Eriksson 2018-04-20 12:28:07 +02:00
parent d2f7811854
commit 3a327c48c8
12 changed files with 154 additions and 3 deletions

View file

@ -147,6 +147,9 @@ sway_cmd cmd_unmark;
sway_cmd cmd_workspace;
sway_cmd cmd_ws_auto_back_and_forth;
sway_cmd cmd_workspace_layout;
sway_cmd cmd_idle_timeout;
sway_cmd cmd_lock_timeout;
sway_cmd cmd_swaylock_command;
sway_cmd bar_cmd_activate_button;
sway_cmd bar_cmd_binding_mode_indicator;

View file

@ -296,6 +296,9 @@ struct sway_config {
enum sway_container_layout default_layout;
char *font;
int font_height;
uint32_t idle_timeout;
uint32_t lock_timeout;
char *swaylock_command;
// Flags
bool focus_follows_mouse;

6
include/sway/idle.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef _SWAY_IDLE_H
#define _SWAY_IDLE_H
#include <sway/server.h>
bool idle_init(struct sway_server *server);
#endif

View file

@ -99,12 +99,15 @@ static struct cmd_handler handlers[] = {
{ "exec", cmd_exec },
{ "exec_always", cmd_exec_always },
{ "focus_follows_mouse", cmd_focus_follows_mouse },
{ "idle_timeout", cmd_idle_timeout },
{ "include", cmd_include },
{ "input", cmd_input },
{ "lock_timeout", cmd_lock_timeout },
{ "mode", cmd_mode },
{ "mouse_warping", cmd_mouse_warping },
{ "output", cmd_output },
{ "seat", cmd_seat },
{ "swaylock_command", cmd_swaylock_command },
{ "workspace", cmd_workspace },
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
};
@ -416,7 +419,7 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) {
struct cmd_handler *handler = find_handler(argv[0], block);
if (!handler) {
char *input = argv[0] ? argv[0] : "(empty)";
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command %d", block);
goto cleanup;
}
int i;

View file

@ -0,0 +1,17 @@
#include <string.h>
#include <strings.h>
#include <errno.h>
#include "sway/commands.h"
struct cmd_results *cmd_idle_timeout(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "idle_timeout", EXPECTED_EQUAL_TO, 1))) {
return error;
}
errno = 0;
config->idle_timeout = strtol(argv[0], NULL, 10);
if (errno == EINVAL || errno == ERANGE)
return cmd_results_new(CMD_INVALID, "idle_timeout", "Invalid timeout '%s'", argv[0]);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -0,0 +1,17 @@
#include <string.h>
#include <strings.h>
#include <errno.h>
#include "sway/commands.h"
struct cmd_results *cmd_lock_timeout(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "lock_timeout", EXPECTED_EQUAL_TO, 1))) {
return error;
}
errno = 0;
config->lock_timeout = strtol(argv[0], NULL, 10);
if (errno == EINVAL || errno == ERANGE)
return cmd_results_new(CMD_INVALID, "lock_timeout", "Invalid lock timeout.");
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -0,0 +1,16 @@
#include <string.h>
#include "sway/commands.h"
#include "log.h"
#include "stringop.h"
struct cmd_results *cmd_swaylock_command(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "swaylock_command", EXPECTED_AT_LEAST, 1))) {
return error;
}
free(config->swaylock_command);
config->swaylock_command = join_args(argv, argc);
wlr_log(L_DEBUG, "Using custom swaylock command: %s",
config->swaylock_command);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -155,6 +155,9 @@ static void config_defaults(struct sway_config *config) {
config->floating_mod = 0;
config->dragging_key = BTN_LEFT;
config->resizing_key = BTN_RIGHT;
config->idle_timeout = 600;
config->lock_timeout = 300;
config->swaylock_command = strdup("swaylock -c 000000");
if (!(config->floating_scroll_up_cmd = strdup(""))) goto cleanup;
if (!(config->floating_scroll_down_cmd = strdup(""))) goto cleanup;
if (!(config->floating_scroll_left_cmd = strdup(""))) goto cleanup;

79
sway/idle.c Normal file
View file

@ -0,0 +1,79 @@
#define _POSIX_C_SOURCE 200112L
#include <sys/types.h>
#include <unistd.h>
#include <wlr/types/wlr_idle.h>
#include <wlr/types/wlr_output.h>
#include <wlr/util/log.h>
#include <sway/server.h>
#include <sway/output.h>
#include <sway/config.h>
void invoke_swaylock() {
int pid = fork();
if (pid == 0) {
char *const cmd[] = { "sh", "-c", config->swaylock_command, NULL, };
execvp(cmd[0], cmd);
exit(1);
}
wlr_log(L_DEBUG, "Spawned swaylock %d", pid);
}
static int handle_idle(void* data) {
wlr_log(L_ERROR, "Idle state");
for (int i = 0; i < root_container.children->length; ++i) {
struct sway_container *cont = root_container.children->items[i];
if (cont->type != C_OUTPUT) {
continue;
}
if (cont->sway_output && cont->sway_output->wlr_output) {
wlr_output_enable(cont->sway_output->wlr_output, false);
}
}
return 0;
}
static int handle_resume(void *data) {
wlr_log(L_ERROR, "Active state");
for (int i = 0; i < root_container.children->length; ++i) {
struct sway_container *cont = root_container.children->items[i];
if (cont->type != C_OUTPUT) {
continue;
}
if (cont->sway_output && cont->sway_output->wlr_output) {
wlr_output_enable(cont->sway_output->wlr_output, true);
}
}
return 0;
}
static const struct wlr_idle_timeout_listener idle_listener = {
.idle = handle_idle,
.resumed = handle_resume,
};
static int handle_lock(void* data) {
wlr_log(L_DEBUG, "Lock screen");
invoke_swaylock();
return 0;
}
static int handle_nop(void *data) {
//NOP
return 0;
}
static const struct wlr_idle_timeout_listener lock_listener = {
.idle = handle_lock,
.resumed = handle_nop,
};
bool idle_init(struct sway_server *server) {
wlr_log(L_DEBUG, "Initializing idle");
server->idle = wlr_idle_create(server->wl_display);
wlr_log(L_DEBUG, "Setup idle timer %d", config->idle_timeout);
wlr_idle_listen(server->idle, config->idle_timeout * 1000, &idle_listener);
wlr_log(L_DEBUG, "Setup lock timer %d", config->lock_timeout);
wlr_idle_listen(server->idle, config->lock_timeout * 1000, &lock_listener);
return true;
}

View file

@ -18,6 +18,7 @@
#include <wlr/util/log.h>
#include "sway/config.h"
#include "sway/debug.h"
#include "sway/idle.h"
#include "sway/server.h"
#include "sway/tree/layout.h"
#include "sway/ipc-server.h"
@ -413,6 +414,7 @@ int main(int argc, char **argv) {
// TODO: wait for server to be ready
// TODO: consume config->cmd_queue
config->active = true;
idle_init(&server);
if (!terminate_request) {
server_run(&server);

View file

@ -1,6 +1,7 @@
sway_sources = files(
'main.c',
'server.c',
'idle.c',
'commands.c',
'config.c',
'criteria.c',
@ -54,6 +55,9 @@ sway_sources = files(
'commands/swaybg_command.c',
'commands/workspace.c',
'commands/ws_auto_back_and_forth.c',
'commands/idle_timeout.c',
'commands/lock_timeout.c',
'commands/swaylock_command.c',
'commands/bar/activate_button.c',
'commands/bar/binding_mode_indicator.c',

View file

@ -16,7 +16,6 @@
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/types/wlr_xdg_output.h>
#include <wlr/types/wlr_wl_shell.h>
#include <wlr/types/wlr_idle.h>
#include <wlr/util/log.h>
// TODO WLR: make Xwayland optional
#include <wlr/xwayland.h>
@ -58,7 +57,6 @@ bool server_init(struct sway_server *server) {
server->data_device_manager =
wlr_data_device_manager_create(server->wl_display);
server->idle = wlr_idle_create(server->wl_display);
wlr_screenshooter_create(server->wl_display);
wlr_gamma_control_manager_create(server->wl_display);
wlr_primary_selection_device_manager_create(server->wl_display);