Skip to content

Commit 2ba821d

Browse files
authored
Merge pull request #7 from ianyfan/wait-option
Add ability to wait for command to finish
2 parents 1fe7145 + 5b653e8 commit 2ba821d

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

main.c

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct swayidle_state {
3232
struct wl_event_loop *event_loop;
3333
struct wl_list timeout_cmds; // struct swayidle_timeout_cmd *
3434
char *lock_cmd;
35+
bool wait;
3536
} state;
3637

3738
struct swayidle_timeout_cmd {
@@ -83,7 +84,9 @@ static void cmd_exec(char *param) {
8384
swayidle_log(LOG_DEBUG, "Cmd exec %s", param);
8485
pid_t pid = fork();
8586
if (pid == 0) {
86-
pid = fork();
87+
if (!state.wait) {
88+
pid = fork();
89+
}
8790
if (pid == 0) {
8891
char *const cmd[] = { "sh", "-c", param, NULL, };
8992
execvp(cmd[0], cmd);
@@ -104,18 +107,8 @@ static void cmd_exec(char *param) {
104107

105108
#if HAVE_SYSTEMD || HAVE_ELOGIND
106109
static int lock_fd = -1;
107-
static int ongoing_fd = -1;
108110
static struct sd_bus *bus = NULL;
109111

110-
static int release_lock(void *data) {
111-
swayidle_log(LOG_INFO, "Releasing sleep lock %d", ongoing_fd);
112-
if (ongoing_fd >= 0) {
113-
close(ongoing_fd);
114-
}
115-
ongoing_fd = -1;
116-
return 0;
117-
}
118-
119112
static void acquire_sleep_lock(void) {
120113
sd_bus_message *msg = NULL;
121114
sd_bus_error error = SD_BUS_ERROR_NULL;
@@ -127,29 +120,27 @@ static void acquire_sleep_lock(void) {
127120
if (ret < 0) {
128121
swayidle_log(LOG_ERROR,
129122
"Failed to send Inhibit signal: %s", error.message);
130-
sd_bus_error_free(&error);
131-
return;
123+
goto cleanup;
132124
}
133125

134126
ret = sd_bus_message_read(msg, "h", &lock_fd);
135127
if (ret < 0) {
136128
errno = -ret;
137129
swayidle_log_errno(LOG_ERROR,
138130
"Failed to parse D-Bus response for Inhibit");
139-
sd_bus_error_free(&error);
140-
sd_bus_message_unref(msg);
141-
return;
142-
} else {
143-
swayidle_log(LOG_INFO, "Got sleep lock: %d", lock_fd);
131+
goto cleanup;
144132
}
145133

146134
// sd_bus_message_unref closes the file descriptor so we need
147135
// to copy it beforehand
148136
lock_fd = fcntl(lock_fd, F_DUPFD_CLOEXEC, 3);
149-
if (lock_fd < 0) {
150-
swayidle_log(LOG_ERROR, "Failed to copy sleep lock fd");
137+
if (lock_fd >= 0) {
138+
swayidle_log(LOG_INFO, "Got sleep lock: %d", lock_fd);
139+
} else {
140+
swayidle_log_errno(LOG_ERROR, "Failed to copy sleep lock fd");
151141
}
152142

143+
cleanup:
153144
sd_bus_error_free(&error);
154145
sd_bus_message_unref(msg);
155146
}
@@ -170,19 +161,17 @@ static int prepare_for_sleep(sd_bus_message *msg, void *userdata,
170161
return 0;
171162
}
172163

173-
ongoing_fd = lock_fd;
174-
175164
if (state.lock_cmd) {
176165
cmd_exec(state.lock_cmd);
177166
}
167+
swayidle_log(LOG_DEBUG, "Prepare for sleep done");
178168

179-
if (ongoing_fd >= 0) {
180-
struct wl_event_source *source =
181-
wl_event_loop_add_timer(state.event_loop, release_lock, NULL);
182-
wl_event_source_timer_update(source, 1000);
169+
swayidle_log(LOG_INFO, "Releasing sleep lock %d", lock_fd);
170+
if (lock_fd >= 0) {
171+
close(lock_fd);
183172
}
173+
lock_fd = -1;
184174

185-
swayidle_log(LOG_DEBUG, "Prepare for sleep done");
186175
return 0;
187176
}
188177

@@ -371,16 +360,20 @@ static int parse_sleep(int argc, char **argv) {
371360

372361
static int parse_args(int argc, char *argv[]) {
373362
int c;
374-
while ((c = getopt(argc, argv, "hd")) != -1) {
363+
while ((c = getopt(argc, argv, "hdw")) != -1) {
375364
switch (c) {
376365
case 'd':
377366
verbosity = LOG_DEBUG;
378367
break;
368+
case 'w':
369+
state.wait = true;
370+
break;
379371
case 'h':
380372
case '?':
381373
printf("Usage: %s [OPTIONS]\n", argv[0]);
382-
printf(" -d\tdebug\n");
383374
printf(" -h\tthis help menu\n");
375+
printf(" -d\tdebug\n");
376+
printf(" -w\twait for command to finish\n");
384377
return 1;
385378
default:
386379
return 1;

swayidle.1.scd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ swayidle - Idle manager for Wayland
1616
*-d*
1717
Enable debug output.
1818

19+
*-w*
20+
Wait for command to finish executing before continuing, helpful for ensuring
21+
that a *before-sleep* command has finished before the system goes to sleep.
22+
23+
Note: using this option causes swayidle to block until the command finishes.
24+
1925
# DESCRIPTION
2026

2127
swayidle listens for idle activity on your Wayland compositor and executes tasks
@@ -36,6 +42,10 @@ Sending SIGUSR1 to swayidle will immediately enter idle state.
3642
If built with systemd support, executes _command_ before systemd puts the
3743
computer to sleep.
3844

45+
Note: this only delays sleeping up to the limit set in *logind.conf(5)* by
46+
the option InhibitDelayMaxSec. A command that has not finished by then will
47+
continue running after resuming from sleep.
48+
3949
All commands are executed in a shell.
4050

4151
# EXAMPLE

0 commit comments

Comments
 (0)