diff options
-rw-r--r-- | completions/bash/swayidle | 48 | ||||
-rw-r--r-- | completions/fish/swayidle.fish | 3 | ||||
-rw-r--r-- | completions/zsh/_swayidle | 22 | ||||
-rw-r--r-- | include/swaybar/bar.h | 1 | ||||
-rw-r--r-- | meson.build | 4 | ||||
-rw-r--r-- | sway/config.c | 14 | ||||
-rw-r--r-- | swaybar/bar.c | 30 | ||||
-rw-r--r-- | swaybar/tray/tray.c | 8 | ||||
-rw-r--r-- | swayidle/main.c | 483 | ||||
-rw-r--r-- | swayidle/meson.build | 27 | ||||
-rw-r--r-- | swayidle/swayidle.1.scd | 63 |
11 files changed, 41 insertions, 662 deletions
diff --git a/completions/bash/swayidle b/completions/bash/swayidle deleted file mode 100644 index a0cdc8b2..00000000 --- a/completions/bash/swayidle +++ /dev/null | |||
@@ -1,48 +0,0 @@ | |||
1 | # swaymsg(1) completion | ||
2 | |||
3 | _swayidle() | ||
4 | { | ||
5 | local cur prev | ||
6 | _get_comp_words_by_ref -n : cur prev | ||
7 | local prev2=${COMP_WORDS[COMP_CWORD-2]} | ||
8 | local prev3=${COMP_WORDS[COMP_CWORD-3]} | ||
9 | |||
10 | events=( | ||
11 | 'timeout' | ||
12 | 'before-sleep' | ||
13 | ) | ||
14 | |||
15 | short=( | ||
16 | -h | ||
17 | -d | ||
18 | ) | ||
19 | |||
20 | if [ "$prev" = timeout ]; then | ||
21 | # timeout <timeout> | ||
22 | return | ||
23 | elif [ "$prev2" = timeout ]; then | ||
24 | # timeout <timeout> <timeout command> | ||
25 | COMPREPLY=($(compgen -c -- "$cur")) | ||
26 | return | ||
27 | elif [ "$prev3" = timeout ]; then | ||
28 | # timeout <timeout> <timeout command> [resume <resume command>] | ||
29 | COMPREPLY=(resume) | ||
30 | # optional argument; no return here as user may skip 'resume' | ||
31 | fi | ||
32 | |||
33 | case "$prev" in | ||
34 | resume) | ||
35 | COMPREPLY=($(compgen -c -- "$cur")) | ||
36 | return | ||
37 | ;; | ||
38 | before-sleep) | ||
39 | COMPREPLY=($(compgen -c -- "$cur")) | ||
40 | return | ||
41 | ;; | ||
42 | esac | ||
43 | |||
44 | COMPREPLY+=($(compgen -W "${events[*]}" -- "$cur")) | ||
45 | COMPREPLY+=($(compgen -W "${short[*]}" -- "$cur")) | ||
46 | |||
47 | } && | ||
48 | complete -F _swayidle swayidle | ||
diff --git a/completions/fish/swayidle.fish b/completions/fish/swayidle.fish deleted file mode 100644 index 71279925..00000000 --- a/completions/fish/swayidle.fish +++ /dev/null | |||
@@ -1,3 +0,0 @@ | |||
1 | # swayidle | ||
2 | complete -c swayidle -s h --description 'show help' | ||
3 | complete -c swayidle -s d --description 'debug' | ||
diff --git a/completions/zsh/_swayidle b/completions/zsh/_swayidle deleted file mode 100644 index b419bc2c..00000000 --- a/completions/zsh/_swayidle +++ /dev/null | |||
@@ -1,22 +0,0 @@ | |||
1 | #compdef swayidle | ||
2 | # | ||
3 | # Completion script for swayidle | ||
4 | # | ||
5 | |||
6 | local events=('timeout:Execute timeout command if there is no activity for timeout seconds' | ||
7 | 'before-sleep:Execute before-sleep command before sleep') | ||
8 | local resume=('resume:Execute command when there is activity again') | ||
9 | |||
10 | if (($#words <= 2)); then | ||
11 | _arguments -C \ | ||
12 | '(-h --help)'{-h,--help}'[Show help message and quit]' \ | ||
13 | '(-d)'-d'[Enable debug output]' | ||
14 | _describe -t "events" 'swayidle' events | ||
15 | |||
16 | elif [[ "$words[-3]" == before-sleep || "$words[-3]" == resume ]]; then | ||
17 | _describe -t "events" 'swayidle' events | ||
18 | |||
19 | elif [[ "$words[-4]" == timeout ]]; then | ||
20 | _describe -t "events" 'swayidle' events | ||
21 | _describe -t "resume" 'swayidle' resume | ||
22 | fi | ||
diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index e377b8de..2d9ba0d9 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h | |||
@@ -61,6 +61,7 @@ struct swaybar_output { | |||
61 | struct wl_list hotspots; // swaybar_hotspot::link | 61 | struct wl_list hotspots; // swaybar_hotspot::link |
62 | 62 | ||
63 | char *name; | 63 | char *name; |
64 | char *identifier; | ||
64 | bool focused; | 65 | bool focused; |
65 | 66 | ||
66 | uint32_t width, height; | 67 | uint32_t width, height; |
diff --git a/meson.build b/meson.build index b1353b59..bffbe312 100644 --- a/meson.build +++ b/meson.build | |||
@@ -86,7 +86,6 @@ if scdoc.found() | |||
86 | 'sway/sway-output.5.scd', | 86 | 'sway/sway-output.5.scd', |
87 | 'swaylock/swaylock.1.scd', | 87 | 'swaylock/swaylock.1.scd', |
88 | 'swaymsg/swaymsg.1.scd', | 88 | 'swaymsg/swaymsg.1.scd', |
89 | 'swayidle/swayidle.1.scd', | ||
90 | 'swaynag/swaynag.1.scd', | 89 | 'swaynag/swaynag.1.scd', |
91 | 'swaynag/swaynag.5.scd', | 90 | 'swaynag/swaynag.5.scd', |
92 | ] | 91 | ] |
@@ -146,7 +145,6 @@ subdir('swaymsg') | |||
146 | subdir('client') | 145 | subdir('client') |
147 | subdir('swaybg') | 146 | subdir('swaybg') |
148 | subdir('swaybar') | 147 | subdir('swaybar') |
149 | subdir('swayidle') | ||
150 | subdir('swaynag') | 148 | subdir('swaynag') |
151 | subdir('swaylock') | 149 | subdir('swaylock') |
152 | 150 | ||
@@ -214,7 +212,6 @@ if (get_option('bash-completions')) | |||
214 | bash_files = files( | 212 | bash_files = files( |
215 | 'completions/bash/sway', | 213 | 'completions/bash/sway', |
216 | 'completions/bash/swaybar', | 214 | 'completions/bash/swaybar', |
217 | 'completions/bash/swayidle', | ||
218 | 'completions/bash/swaylock', | 215 | 'completions/bash/swaylock', |
219 | 'completions/bash/swaymsg', | 216 | 'completions/bash/swaymsg', |
220 | ) | 217 | ) |
@@ -226,7 +223,6 @@ endif | |||
226 | if (get_option('fish-completions')) | 223 | if (get_option('fish-completions')) |
227 | fish_files = files( | 224 | fish_files = files( |
228 | 'completions/fish/sway.fish', | 225 | 'completions/fish/sway.fish', |
229 | 'completions/fish/swayidle.fish', | ||
230 | 'completions/fish/swaylock.fish', | 226 | 'completions/fish/swaylock.fish', |
231 | 'completions/fish/swaymsg.fish', | 227 | 'completions/fish/swaymsg.fish', |
232 | 'completions/fish/swaynag.fish', | 228 | 'completions/fish/swaynag.fish', |
diff --git a/sway/config.c b/sway/config.c index ea9f23dd..b14ebba7 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -571,15 +571,18 @@ bool load_include_configs(const char *path, struct sway_config *config, | |||
571 | } | 571 | } |
572 | 572 | ||
573 | // get line, with backslash continuation | 573 | // get line, with backslash continuation |
574 | static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file) { | 574 | static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file, |
575 | int *nlines) { | ||
575 | char *next_line = NULL; | 576 | char *next_line = NULL; |
576 | size_t next_line_size = 0; | 577 | size_t next_line_size = 0; |
577 | ssize_t nread = getline(lineptr, line_size, file); | 578 | ssize_t nread = getline(lineptr, line_size, file); |
579 | *nlines = nread == -1 ? 0 : 1; | ||
578 | while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) { | 580 | while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) { |
579 | ssize_t next_nread = getline(&next_line, &next_line_size, file); | 581 | ssize_t next_nread = getline(&next_line, &next_line_size, file); |
580 | if (next_nread == -1) { | 582 | if (next_nread == -1) { |
581 | break; | 583 | break; |
582 | } | 584 | } |
585 | (*nlines)++; | ||
583 | 586 | ||
584 | nread += next_nread - 2; | 587 | nread += next_nread - 2; |
585 | if ((ssize_t) *line_size < nread + 1) { | 588 | if ((ssize_t) *line_size < nread + 1) { |
@@ -613,7 +616,9 @@ static int detect_brace(FILE *file) { | |||
613 | } | 616 | } |
614 | } | 617 | } |
615 | free(line); | 618 | free(line); |
616 | fseek(file, pos, SEEK_SET); | 619 | if (ret == 0) { |
620 | fseek(file, pos, SEEK_SET); | ||
621 | } | ||
617 | return ret; | 622 | return ret; |
618 | } | 623 | } |
619 | 624 | ||
@@ -661,7 +666,8 @@ bool read_config(FILE *file, struct sway_config *config, | |||
661 | ssize_t nread; | 666 | ssize_t nread; |
662 | list_t *stack = create_list(); | 667 | list_t *stack = create_list(); |
663 | size_t read = 0; | 668 | size_t read = 0; |
664 | while ((nread = getline_with_cont(&line, &line_size, file)) != -1) { | 669 | int nlines = 0; |
670 | while ((nread = getline_with_cont(&line, &line_size, file, &nlines)) != -1) { | ||
665 | if (reading_main_config) { | 671 | if (reading_main_config) { |
666 | if (read + nread > config_size) { | 672 | if (read + nread > config_size) { |
667 | wlr_log(WLR_ERROR, "Config file changed during reading"); | 673 | wlr_log(WLR_ERROR, "Config file changed during reading"); |
@@ -677,7 +683,7 @@ bool read_config(FILE *file, struct sway_config *config, | |||
677 | line[nread - 1] = '\0'; | 683 | line[nread - 1] = '\0'; |
678 | } | 684 | } |
679 | 685 | ||
680 | line_number++; | 686 | line_number += nlines; |
681 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); | 687 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); |
682 | 688 | ||
683 | strip_whitespace(line); | 689 | strip_whitespace(line); |
diff --git a/swaybar/bar.c b/swaybar/bar.c index 7aed4dca..d36367fc 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c | |||
@@ -59,6 +59,7 @@ static void swaybar_output_free(struct swaybar_output *output) { | |||
59 | free_workspaces(&output->workspaces); | 59 | free_workspaces(&output->workspaces); |
60 | wl_list_remove(&output->link); | 60 | wl_list_remove(&output->link); |
61 | free(output->name); | 61 | free(output->name); |
62 | free(output->identifier); | ||
62 | free(output); | 63 | free(output); |
63 | } | 64 | } |
64 | 65 | ||
@@ -166,13 +167,15 @@ bool determine_bar_visibility(struct swaybar *bar, bool moving_layer) { | |||
166 | return visible; | 167 | return visible; |
167 | } | 168 | } |
168 | 169 | ||
169 | static bool bar_uses_output(struct swaybar *bar, const char *name) { | 170 | static bool bar_uses_output(struct swaybar_output *output) { |
170 | if (bar->config->all_outputs) { | 171 | if (output->bar->config->all_outputs) { |
171 | return true; | 172 | return true; |
172 | } | 173 | } |
174 | char *identifier = output->identifier; | ||
173 | struct config_output *coutput; | 175 | struct config_output *coutput; |
174 | wl_list_for_each(coutput, &bar->config->outputs, link) { | 176 | wl_list_for_each(coutput, &output->bar->config->outputs, link) { |
175 | if (strcmp(coutput->name, name) == 0) { | 177 | if (strcmp(coutput->name, output->name) == 0 || |
178 | (identifier && strcmp(coutput->name, identifier) == 0)) { | ||
176 | return true; | 179 | return true; |
177 | } | 180 | } |
178 | } | 181 | } |
@@ -233,7 +236,7 @@ static void xdg_output_handle_done(void *data, | |||
233 | struct swaybar *bar = output->bar; | 236 | struct swaybar *bar = output->bar; |
234 | 237 | ||
235 | assert(output->name != NULL); | 238 | assert(output->name != NULL); |
236 | if (!bar_uses_output(bar, output->name)) { | 239 | if (!bar_uses_output(output)) { |
237 | swaybar_output_free(output); | 240 | swaybar_output_free(output); |
238 | return; | 241 | return; |
239 | } | 242 | } |
@@ -258,7 +261,22 @@ static void xdg_output_handle_name(void *data, | |||
258 | 261 | ||
259 | static void xdg_output_handle_description(void *data, | 262 | static void xdg_output_handle_description(void *data, |
260 | struct zxdg_output_v1 *xdg_output, const char *description) { | 263 | struct zxdg_output_v1 *xdg_output, const char *description) { |
261 | // Who cares | 264 | // wlroots currently sets the description to `make model serial (name)` |
265 | // If this changes in the future, this will need to be modified. | ||
266 | struct swaybar_output *output = data; | ||
267 | free(output->identifier); | ||
268 | output->identifier = NULL; | ||
269 | char *paren = strrchr(description, '('); | ||
270 | if (paren) { | ||
271 | size_t length = paren - description; | ||
272 | output->identifier = malloc(length); | ||
273 | if (!output->identifier) { | ||
274 | wlr_log(WLR_ERROR, "Failed to allocate output identifier"); | ||
275 | return; | ||
276 | } | ||
277 | strncpy(output->identifier, description, length); | ||
278 | output->identifier[length - 1] = '\0'; | ||
279 | } | ||
262 | } | 280 | } |
263 | 281 | ||
264 | struct zxdg_output_v1_listener xdg_output_listener = { | 282 | struct zxdg_output_v1_listener xdg_output_listener = { |
diff --git a/swaybar/tray/tray.c b/swaybar/tray/tray.c index 0c3517cb..d5d0c84e 100644 --- a/swaybar/tray/tray.c +++ b/swaybar/tray/tray.c | |||
@@ -101,13 +101,17 @@ void tray_in(int fd, short mask, void *data) { | |||
101 | } | 101 | } |
102 | 102 | ||
103 | static int cmp_output(const void *item, const void *cmp_to) { | 103 | static int cmp_output(const void *item, const void *cmp_to) { |
104 | return strcmp(item, cmp_to); | 104 | const struct swaybar_output *output = cmp_to; |
105 | if (output->identifier && strcmp(item, output->identifier) == 0) { | ||
106 | return 0; | ||
107 | } | ||
108 | return strcmp(item, output->name); | ||
105 | } | 109 | } |
106 | 110 | ||
107 | uint32_t render_tray(cairo_t *cairo, struct swaybar_output *output, double *x) { | 111 | uint32_t render_tray(cairo_t *cairo, struct swaybar_output *output, double *x) { |
108 | struct swaybar_config *config = output->bar->config; | 112 | struct swaybar_config *config = output->bar->config; |
109 | if (config->tray_outputs) { | 113 | if (config->tray_outputs) { |
110 | if (list_seq_find(config->tray_outputs, cmp_output, output->name) == -1) { | 114 | if (list_seq_find(config->tray_outputs, cmp_output, output) == -1) { |
111 | return 0; | 115 | return 0; |
112 | } | 116 | } |
113 | } // else display on all | 117 | } // else display on all |
diff --git a/swayidle/main.c b/swayidle/main.c deleted file mode 100644 index 41eecc41..00000000 --- a/swayidle/main.c +++ /dev/null | |||
@@ -1,483 +0,0 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include <assert.h> | ||
3 | #include <errno.h> | ||
4 | #include <fcntl.h> | ||
5 | #include <getopt.h> | ||
6 | #include <pthread.h> | ||
7 | #include <signal.h> | ||
8 | #include <stdio.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <string.h> | ||
11 | #include <sys/wait.h> | ||
12 | #include <unistd.h> | ||
13 | #include <wayland-client-protocol.h> | ||
14 | #include <wayland-client.h> | ||
15 | #include <wayland-server.h> | ||
16 | #include <wayland-util.h> | ||
17 | #include <wlr/config.h> | ||
18 | #include <wlr/util/log.h> | ||
19 | #include "config.h" | ||
20 | #include "idle-client-protocol.h" | ||
21 | #include "list.h" | ||
22 | #if HAVE_SYSTEMD | ||
23 | #include <systemd/sd-bus.h> | ||
24 | #include <systemd/sd-login.h> | ||
25 | #elif HAVE_ELOGIND | ||
26 | #include <elogind/sd-bus.h> | ||
27 | #include <elogind/sd-login.h> | ||
28 | #endif | ||
29 | |||
30 | static struct org_kde_kwin_idle *idle_manager = NULL; | ||
31 | static struct wl_seat *seat = NULL; | ||
32 | |||
33 | struct swayidle_state { | ||
34 | struct wl_display *display; | ||
35 | struct wl_event_loop *event_loop; | ||
36 | list_t *timeout_cmds; // struct swayidle_timeout_cmd * | ||
37 | char *lock_cmd; | ||
38 | } state; | ||
39 | |||
40 | struct swayidle_timeout_cmd { | ||
41 | int timeout, registered_timeout; | ||
42 | struct org_kde_kwin_idle_timeout *idle_timer; | ||
43 | char *idle_cmd; | ||
44 | char *resume_cmd; | ||
45 | }; | ||
46 | |||
47 | void sway_terminate(int exit_code) { | ||
48 | wl_display_disconnect(state.display); | ||
49 | wl_event_loop_destroy(state.event_loop); | ||
50 | exit(exit_code); | ||
51 | } | ||
52 | |||
53 | static void cmd_exec(char *param) { | ||
54 | wlr_log(WLR_DEBUG, "Cmd exec %s", param); | ||
55 | pid_t pid = fork(); | ||
56 | if (pid == 0) { | ||
57 | pid = fork(); | ||
58 | if (pid == 0) { | ||
59 | char *const cmd[] = { "sh", "-c", param, NULL, }; | ||
60 | execvp(cmd[0], cmd); | ||
61 | wlr_log_errno(WLR_ERROR, "execve failed!"); | ||
62 | exit(1); | ||
63 | } else if (pid < 0) { | ||
64 | wlr_log_errno(WLR_ERROR, "fork failed"); | ||
65 | exit(1); | ||
66 | } | ||
67 | exit(0); | ||
68 | } else if (pid < 0) { | ||
69 | wlr_log_errno(WLR_ERROR, "fork failed"); | ||
70 | } else { | ||
71 | wlr_log(WLR_DEBUG, "Spawned process %s", param); | ||
72 | waitpid(pid, NULL, 0); | ||
73 | } | ||
74 | } | ||
75 | |||
76 | #if HAVE_SYSTEMD || HAVE_ELOGIND | ||
77 | static int lock_fd = -1; | ||
78 | static int ongoing_fd = -1; | ||
79 | static struct sd_bus *bus = NULL; | ||
80 | |||
81 | static int release_lock(void *data) { | ||
82 | wlr_log(WLR_INFO, "Releasing sleep lock %d", ongoing_fd); | ||
83 | if (ongoing_fd >= 0) { | ||
84 | close(ongoing_fd); | ||
85 | } | ||
86 | ongoing_fd = -1; | ||
87 | return 0; | ||
88 | } | ||
89 | |||
90 | static void acquire_sleep_lock(void) { | ||
91 | sd_bus_message *msg = NULL; | ||
92 | sd_bus_error error = SD_BUS_ERROR_NULL; | ||
93 | int ret = sd_bus_call_method(bus, "org.freedesktop.login1", | ||
94 | "/org/freedesktop/login1", | ||
95 | "org.freedesktop.login1.Manager", "Inhibit", | ||
96 | &error, &msg, "ssss", "sleep", "swayidle", | ||
97 | "Setup Up Lock Screen", "delay"); | ||
98 | if (ret < 0) { | ||
99 | wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s", error.message); | ||
100 | sd_bus_error_free(&error); | ||
101 | return; | ||
102 | } | ||
103 | |||
104 | ret = sd_bus_message_read(msg, "h", &lock_fd); | ||
105 | if (ret < 0) { | ||
106 | wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s", | ||
107 | strerror(-ret)); | ||
108 | sd_bus_error_free(&error); | ||
109 | sd_bus_message_unref(msg); | ||
110 | return; | ||
111 | } else { | ||
112 | wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd); | ||
113 | } | ||
114 | |||
115 | // sd_bus_message_unref closes the file descriptor so we need | ||
116 | // to copy it beforehand | ||
117 | lock_fd = fcntl(lock_fd, F_DUPFD_CLOEXEC, 3); | ||
118 | if (lock_fd < 0) { | ||
119 | wlr_log(WLR_ERROR, "Failed to copy sleep lock fd: %s", | ||
120 | strerror(errno)); | ||
121 | } | ||
122 | |||
123 | sd_bus_error_free(&error); | ||
124 | sd_bus_message_unref(msg); | ||
125 | } | ||
126 | |||
127 | static int prepare_for_sleep(sd_bus_message *msg, void *userdata, | ||
128 | sd_bus_error *ret_error) { | ||
129 | /* "b" apparently reads into an int, not a bool */ | ||
130 | int going_down = 1; | ||
131 | int ret = sd_bus_message_read(msg, "b", &going_down); | ||
132 | if (ret < 0) { | ||
133 | wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s", | ||
134 | strerror(-ret)); | ||
135 | } | ||
136 | wlr_log(WLR_DEBUG, "PrepareForSleep signal received %d", going_down); | ||
137 | if (!going_down) { | ||
138 | acquire_sleep_lock(); | ||
139 | return 0; | ||
140 | } | ||
141 | |||
142 | ongoing_fd = lock_fd; | ||
143 | |||
144 | if (state.lock_cmd) { | ||
145 | cmd_exec(state.lock_cmd); | ||
146 | } | ||
147 | |||
148 | if (ongoing_fd >= 0) { | ||
149 | struct wl_event_source *source = | ||
150 | wl_event_loop_add_timer(state.event_loop, release_lock, NULL); | ||
151 | wl_event_source_timer_update(source, 1000); | ||
152 | } | ||
153 | |||
154 | wlr_log(WLR_DEBUG, "Prepare for sleep done"); | ||
155 | return 0; | ||
156 | } | ||
157 | |||
158 | static int dbus_event(int fd, uint32_t mask, void *data) { | ||
159 | sd_bus *bus = data; | ||
160 | |||
161 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { | ||
162 | sway_terminate(0); | ||
163 | } | ||
164 | |||
165 | int count = 0; | ||
166 | if (mask & WL_EVENT_READABLE) { | ||
167 | count = sd_bus_process(bus, NULL); | ||
168 | } | ||
169 | if (mask & WL_EVENT_WRITABLE) { | ||
170 | sd_bus_flush(bus); | ||
171 | } | ||
172 | if (mask == 0) { | ||
173 | sd_bus_flush(bus); | ||
174 | } | ||
175 | |||
176 | if (count < 0) { | ||
177 | wlr_log_errno(WLR_ERROR, "sd_bus_process failed, exiting"); | ||
178 | sway_terminate(0); | ||
179 | } | ||
180 | |||
181 | return count; | ||
182 | } | ||
183 | |||
184 | static void setup_sleep_listener(void) { | ||
185 | int ret = sd_bus_default_system(&bus); | ||
186 | if (ret < 0) { | ||
187 | wlr_log(WLR_ERROR, "Failed to open D-Bus connection: %s", | ||
188 | strerror(-ret)); | ||
189 | return; | ||
190 | } | ||
191 | |||
192 | char str[256]; | ||
193 | const char *fmt = "type='signal'," | ||
194 | "sender='org.freedesktop.login1'," | ||
195 | "interface='org.freedesktop.login1.%s'," | ||
196 | "member='%s'," "path='%s'"; | ||
197 | |||
198 | snprintf(str, sizeof(str), fmt, "Manager", "PrepareForSleep", | ||
199 | "/org/freedesktop/login1"); | ||
200 | ret = sd_bus_add_match(bus, NULL, str, prepare_for_sleep, NULL); | ||
201 | if (ret < 0) { | ||
202 | wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); | ||
203 | return; | ||
204 | } | ||
205 | acquire_sleep_lock(); | ||
206 | |||
207 | struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop, | ||
208 | sd_bus_get_fd(bus), WL_EVENT_READABLE, dbus_event, bus); | ||
209 | wl_event_source_check(source); | ||
210 | } | ||
211 | #endif | ||
212 | |||
213 | static void handle_global(void *data, struct wl_registry *registry, | ||
214 | uint32_t name, const char *interface, uint32_t version) { | ||
215 | if (strcmp(interface, org_kde_kwin_idle_interface.name) == 0) { | ||
216 | idle_manager = | ||
217 | wl_registry_bind(registry, name, &org_kde_kwin_idle_interface, 1); | ||
218 | } else if (strcmp(interface, wl_seat_interface.name) == 0) { | ||
219 | seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); | ||
220 | } | ||
221 | } | ||
222 | |||
223 | static void handle_global_remove(void *data, struct wl_registry *registry, | ||
224 | uint32_t name) { | ||
225 | // Who cares | ||
226 | } | ||
227 | |||
228 | static const struct wl_registry_listener registry_listener = { | ||
229 | .global = handle_global, | ||
230 | .global_remove = handle_global_remove, | ||
231 | }; | ||
232 | |||
233 | static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener; | ||
234 | |||
235 | static void register_timeout(struct swayidle_timeout_cmd *cmd, | ||
236 | int timeout) { | ||
237 | if (cmd->idle_timer != NULL) { | ||
238 | org_kde_kwin_idle_timeout_destroy(cmd->idle_timer); | ||
239 | cmd->idle_timer = NULL; | ||
240 | } | ||
241 | if (timeout < 0) { | ||
242 | wlr_log(WLR_DEBUG, "Not registering idle timeout"); | ||
243 | return; | ||
244 | } | ||
245 | wlr_log(WLR_DEBUG, "Register with timeout: %d", timeout); | ||
246 | cmd->idle_timer = | ||
247 | org_kde_kwin_idle_get_idle_timeout(idle_manager, seat, timeout); | ||
248 | org_kde_kwin_idle_timeout_add_listener(cmd->idle_timer, | ||
249 | &idle_timer_listener, cmd); | ||
250 | cmd->registered_timeout = timeout; | ||
251 | } | ||
252 | |||
253 | static void handle_idle(void *data, struct org_kde_kwin_idle_timeout *timer) { | ||
254 | struct swayidle_timeout_cmd *cmd = data; | ||
255 | wlr_log(WLR_DEBUG, "idle state"); | ||
256 | if (cmd->idle_cmd) { | ||
257 | cmd_exec(cmd->idle_cmd); | ||
258 | } | ||
259 | } | ||
260 | |||
261 | static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) { | ||
262 | struct swayidle_timeout_cmd *cmd = data; | ||
263 | wlr_log(WLR_DEBUG, "active state"); | ||
264 | if (cmd->registered_timeout != cmd->timeout) { | ||
265 | register_timeout(cmd, cmd->timeout); | ||
266 | } | ||
267 | if (cmd->resume_cmd) { | ||
268 | cmd_exec(cmd->resume_cmd); | ||
269 | } | ||
270 | } | ||
271 | |||
272 | static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = { | ||
273 | .idle = handle_idle, | ||
274 | .resumed = handle_resume, | ||
275 | }; | ||
276 | |||
277 | static char *parse_command(int argc, char **argv) { | ||
278 | if (argc < 1) { | ||
279 | wlr_log(WLR_ERROR, "Missing command"); | ||
280 | return NULL; | ||
281 | } | ||
282 | |||
283 | wlr_log(WLR_DEBUG, "Command: %s", argv[0]); | ||
284 | return strdup(argv[0]); | ||
285 | } | ||
286 | |||
287 | static int parse_timeout(int argc, char **argv) { | ||
288 | if (argc < 3) { | ||
289 | wlr_log(WLR_ERROR, "Too few parameters to timeout command. " | ||
290 | "Usage: timeout <seconds> <command>"); | ||
291 | exit(-1); | ||
292 | } | ||
293 | errno = 0; | ||
294 | char *endptr; | ||
295 | int seconds = strtoul(argv[1], &endptr, 10); | ||
296 | if (errno != 0 || *endptr != '\0') { | ||
297 | wlr_log(WLR_ERROR, "Invalid timeout parameter '%s', it should be a " | ||
298 | "numeric value representing seconds", optarg); | ||
299 | exit(-1); | ||
300 | } | ||
301 | |||
302 | struct swayidle_timeout_cmd *cmd = | ||
303 | calloc(1, sizeof(struct swayidle_timeout_cmd)); | ||
304 | |||
305 | if (seconds > 0) { | ||
306 | cmd->timeout = seconds * 1000; | ||
307 | } else { | ||
308 | cmd->timeout = -1; | ||
309 | } | ||
310 | |||
311 | wlr_log(WLR_DEBUG, "Register idle timeout at %d ms", cmd->timeout); | ||
312 | wlr_log(WLR_DEBUG, "Setup idle"); | ||
313 | cmd->idle_cmd = parse_command(argc - 2, &argv[2]); | ||
314 | |||
315 | int result = 3; | ||
316 | if (argc >= 5 && !strcmp("resume", argv[3])) { | ||
317 | wlr_log(WLR_DEBUG, "Setup resume"); | ||
318 | cmd->resume_cmd = parse_command(argc - 4, &argv[4]); | ||
319 | result = 5; | ||
320 | } | ||
321 | list_add(state.timeout_cmds, cmd); | ||
322 | return result; | ||
323 | } | ||
324 | |||
325 | static int parse_sleep(int argc, char **argv) { | ||
326 | if (argc < 2) { | ||
327 | wlr_log(WLR_ERROR, "Too few parameters to before-sleep command. " | ||
328 | "Usage: before-sleep <command>"); | ||
329 | exit(-1); | ||
330 | } | ||
331 | |||
332 | state.lock_cmd = parse_command(argc - 1, &argv[1]); | ||
333 | if (state.lock_cmd) { | ||
334 | wlr_log(WLR_DEBUG, "Setup sleep lock: %s", state.lock_cmd); | ||
335 | } | ||
336 | |||
337 | return 2; | ||
338 | } | ||
339 | |||
340 | static int parse_args(int argc, char *argv[]) { | ||
341 | bool debug = false; | ||
342 | |||
343 | int c; | ||
344 | while ((c = getopt(argc, argv, "hd")) != -1) { | ||
345 | switch (c) { | ||
346 | case 'd': | ||
347 | debug = true; | ||
348 | break; | ||
349 | case 'h': | ||
350 | case '?': | ||
351 | printf("Usage: %s [OPTIONS]\n", argv[0]); | ||
352 | printf(" -d\tdebug\n"); | ||
353 | printf(" -h\tthis help menu\n"); | ||
354 | return 1; | ||
355 | default: | ||
356 | return 1; | ||
357 | } | ||
358 | } | ||
359 | |||
360 | wlr_log_init(debug ? WLR_DEBUG : WLR_INFO, NULL); | ||
361 | |||
362 | state.timeout_cmds = create_list(); | ||
363 | |||
364 | int i = optind; | ||
365 | while (i < argc) { | ||
366 | if (!strcmp("timeout", argv[i])) { | ||
367 | wlr_log(WLR_DEBUG, "Got timeout"); | ||
368 | i += parse_timeout(argc - i, &argv[i]); | ||
369 | } else if (!strcmp("before-sleep", argv[i])) { | ||
370 | wlr_log(WLR_DEBUG, "Got before-sleep"); | ||
371 | i += parse_sleep(argc - i, &argv[i]); | ||
372 | } else { | ||
373 | wlr_log(WLR_ERROR, "Unsupported command '%s'", argv[i]); | ||
374 | return 1; | ||
375 | } | ||
376 | } | ||
377 | |||
378 | return 0; | ||
379 | } | ||
380 | |||
381 | static int handle_signal(int sig, void *data) { | ||
382 | switch (sig) { | ||
383 | case SIGINT: | ||
384 | case SIGTERM: | ||
385 | sway_terminate(0); | ||
386 | return 0; | ||
387 | case SIGUSR1: | ||
388 | wlr_log(WLR_DEBUG, "Got SIGUSR1"); | ||
389 | for (int i = 0; i < state.timeout_cmds->length; ++i) { | ||
390 | register_timeout(state.timeout_cmds->items[i], 0); | ||
391 | } | ||
392 | return 1; | ||
393 | } | ||
394 | assert(false); // not reached | ||
395 | } | ||
396 | |||
397 | static int display_event(int fd, uint32_t mask, void *data) { | ||
398 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { | ||
399 | sway_terminate(0); | ||
400 | } | ||
401 | |||
402 | int count = 0; | ||
403 | if (mask & WL_EVENT_READABLE) { | ||
404 | count = wl_display_dispatch(state.display); | ||
405 | } | ||
406 | if (mask & WL_EVENT_WRITABLE) { | ||
407 | wl_display_flush(state.display); | ||
408 | } | ||
409 | if (mask == 0) { | ||
410 | count = wl_display_dispatch_pending(state.display); | ||
411 | wl_display_flush(state.display); | ||
412 | } | ||
413 | |||
414 | if (count < 0) { | ||
415 | wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting"); | ||
416 | sway_terminate(0); | ||
417 | } | ||
418 | |||
419 | return count; | ||
420 | } | ||
421 | |||
422 | int main(int argc, char *argv[]) { | ||
423 | if (parse_args(argc, argv) != 0) { | ||
424 | return -1; | ||
425 | } | ||
426 | |||
427 | state.event_loop = wl_event_loop_create(); | ||
428 | |||
429 | wl_event_loop_add_signal(state.event_loop, SIGINT, handle_signal, NULL); | ||
430 | wl_event_loop_add_signal(state.event_loop, SIGTERM, handle_signal, NULL); | ||
431 | wl_event_loop_add_signal(state.event_loop, SIGUSR1, handle_signal, NULL); | ||
432 | |||
433 | state.display = wl_display_connect(NULL); | ||
434 | if (state.display == NULL) { | ||
435 | wlr_log(WLR_ERROR, "Unable to connect to the compositor. " | ||
436 | "If your compositor is running, check or set the " | ||
437 | "WAYLAND_DISPLAY environment variable."); | ||
438 | return -3; | ||
439 | } | ||
440 | |||
441 | struct wl_registry *registry = wl_display_get_registry(state.display); | ||
442 | wl_registry_add_listener(registry, ®istry_listener, NULL); | ||
443 | wl_display_roundtrip(state.display); | ||
444 | |||
445 | if (idle_manager == NULL) { | ||
446 | wlr_log(WLR_ERROR, "Display doesn't support idle protocol"); | ||
447 | return -4; | ||
448 | } | ||
449 | if (seat == NULL) { | ||
450 | wlr_log(WLR_ERROR, "Seat error"); | ||
451 | return -5; | ||
452 | } | ||
453 | |||
454 | bool should_run = state.timeout_cmds->length > 0; | ||
455 | #if HAVE_SYSTEMD || HAVE_ELOGIND | ||
456 | if (state.lock_cmd) { | ||
457 | should_run = true; | ||
458 | setup_sleep_listener(); | ||
459 | } | ||
460 | #endif | ||
461 | if (!should_run) { | ||
462 | wlr_log(WLR_INFO, "No command specified! Nothing to do, will exit"); | ||
463 | sway_terminate(0); | ||
464 | } | ||
465 | |||
466 | for (int i = 0; i < state.timeout_cmds->length; ++i) { | ||
467 | struct swayidle_timeout_cmd *cmd = state.timeout_cmds->items[i]; | ||
468 | register_timeout(cmd, cmd->timeout); | ||
469 | } | ||
470 | |||
471 | wl_display_roundtrip(state.display); | ||
472 | |||
473 | struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop, | ||
474 | wl_display_get_fd(state.display), WL_EVENT_READABLE, | ||
475 | display_event, NULL); | ||
476 | wl_event_source_check(source); | ||
477 | |||
478 | while (wl_event_loop_dispatch(state.event_loop, -1) != 1) { | ||
479 | // This space intentionally left blank | ||
480 | } | ||
481 | |||
482 | sway_terminate(0); | ||
483 | } | ||
diff --git a/swayidle/meson.build b/swayidle/meson.build deleted file mode 100644 index 79d2c5c4..00000000 --- a/swayidle/meson.build +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
1 | threads = dependency('threads') | ||
2 | |||
3 | swayidle_deps = [ | ||
4 | client_protos, | ||
5 | pixman, | ||
6 | wayland_client, | ||
7 | wayland_server, | ||
8 | wlroots, | ||
9 | ] | ||
10 | |||
11 | if systemd.found() | ||
12 | swayidle_deps += systemd | ||
13 | endif | ||
14 | if elogind.found() | ||
15 | swayidle_deps += elogind | ||
16 | endif | ||
17 | |||
18 | executable( | ||
19 | 'swayidle', [ | ||
20 | 'main.c', | ||
21 | ], | ||
22 | include_directories: [sway_inc], | ||
23 | dependencies: swayidle_deps, | ||
24 | link_with: [lib_sway_common, lib_sway_client], | ||
25 | install_rpath : rpathdir, | ||
26 | install: true | ||
27 | ) | ||
diff --git a/swayidle/swayidle.1.scd b/swayidle/swayidle.1.scd deleted file mode 100644 index 0e3b5c3c..00000000 --- a/swayidle/swayidle.1.scd +++ /dev/null | |||
@@ -1,63 +0,0 @@ | |||
1 | swayidle (1) | ||
2 | |||
3 | # NAME | ||
4 | |||
5 | swayidle - Idle manager for Wayland | ||
6 | |||
7 | # SYNOPSIS | ||
8 | |||
9 | *swayidle* [options] [events...] | ||
10 | |||
11 | # OPTIONS | ||
12 | |||
13 | *-h* | ||
14 | Show help message and quit. | ||
15 | |||
16 | *-d* | ||
17 | Enable debug output. | ||
18 | |||
19 | # DESCRIPTION | ||
20 | |||
21 | swayidle listens for idle activity on your Wayland compositor and executes tasks | ||
22 | on various idle-related events. You can specify any number of events at the | ||
23 | command line. | ||
24 | |||
25 | Sending SIGUSR1 to swayidle will immediately enter idle state. | ||
26 | |||
27 | # EVENTS | ||
28 | |||
29 | *timeout* <timeout> <timeout command> [resume <resume command>] | ||
30 | Execute _timeout command_ if there is no activity for <timeout> seconds. | ||
31 | |||
32 | If you specify "resume <resume command>", _resume command_ will be run when | ||
33 | there is activity again. | ||
34 | |||
35 | *before-sleep* <command> | ||
36 | If built with systemd support, executes _command_ before systemd puts the | ||
37 | computer to sleep. | ||
38 | |||
39 | All commands are executed in a shell. | ||
40 | |||
41 | # EXAMPLE | ||
42 | |||
43 | ``` | ||
44 | swayidle \ | ||
45 | timeout 300 'swaylock -c 000000' \ | ||
46 | timeout 600 'swaymsg "output * dpms off"' \ | ||
47 | resume 'swaymsg "output * dpms on"' \ | ||
48 | before-sleep 'swaylock -c 000000' | ||
49 | ``` | ||
50 | |||
51 | This will lock your screen after 300 seconds of inactivity, then turn off your | ||
52 | displays after another 300 seconds, and turn your screens back on when resumed. | ||
53 | It will also lock your screen before your computer goes to sleep. | ||
54 | |||
55 | # AUTHORS | ||
56 | |||
57 | Maintained by Drew DeVault <sir@cmpwn.com>, who is assisted by other open | ||
58 | source contributors. For more information about sway development, see | ||
59 | https://github.com/swaywm/sway. | ||
60 | |||
61 | # SEE ALSO | ||
62 | |||
63 | *sway*(5) *swaymsg*(1) *sway-input*(5) *sway-output*(5) *sway-bar*(5) | ||