@@ -32,6 +32,7 @@ struct swayidle_state {
32
32
struct wl_event_loop * event_loop ;
33
33
struct wl_list timeout_cmds ; // struct swayidle_timeout_cmd *
34
34
char * lock_cmd ;
35
+ bool wait ;
35
36
} state ;
36
37
37
38
struct swayidle_timeout_cmd {
@@ -83,7 +84,9 @@ static void cmd_exec(char *param) {
83
84
swayidle_log (LOG_DEBUG , "Cmd exec %s" , param );
84
85
pid_t pid = fork ();
85
86
if (pid == 0 ) {
86
- pid = fork ();
87
+ if (!state .wait ) {
88
+ pid = fork ();
89
+ }
87
90
if (pid == 0 ) {
88
91
char * const cmd [] = { "sh" , "-c" , param , NULL , };
89
92
execvp (cmd [0 ], cmd );
@@ -104,18 +107,8 @@ static void cmd_exec(char *param) {
104
107
105
108
#if HAVE_SYSTEMD || HAVE_ELOGIND
106
109
static int lock_fd = -1 ;
107
- static int ongoing_fd = -1 ;
108
110
static struct sd_bus * bus = NULL ;
109
111
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
-
119
112
static void acquire_sleep_lock (void ) {
120
113
sd_bus_message * msg = NULL ;
121
114
sd_bus_error error = SD_BUS_ERROR_NULL ;
@@ -127,29 +120,27 @@ static void acquire_sleep_lock(void) {
127
120
if (ret < 0 ) {
128
121
swayidle_log (LOG_ERROR ,
129
122
"Failed to send Inhibit signal: %s" , error .message );
130
- sd_bus_error_free (& error );
131
- return ;
123
+ goto cleanup ;
132
124
}
133
125
134
126
ret = sd_bus_message_read (msg , "h" , & lock_fd );
135
127
if (ret < 0 ) {
136
128
errno = - ret ;
137
129
swayidle_log_errno (LOG_ERROR ,
138
130
"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 ;
144
132
}
145
133
146
134
// sd_bus_message_unref closes the file descriptor so we need
147
135
// to copy it beforehand
148
136
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" );
151
141
}
152
142
143
+ cleanup :
153
144
sd_bus_error_free (& error );
154
145
sd_bus_message_unref (msg );
155
146
}
@@ -170,19 +161,17 @@ static int prepare_for_sleep(sd_bus_message *msg, void *userdata,
170
161
return 0 ;
171
162
}
172
163
173
- ongoing_fd = lock_fd ;
174
-
175
164
if (state .lock_cmd ) {
176
165
cmd_exec (state .lock_cmd );
177
166
}
167
+ swayidle_log (LOG_DEBUG , "Prepare for sleep done" );
178
168
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 );
183
172
}
173
+ lock_fd = -1 ;
184
174
185
- swayidle_log (LOG_DEBUG , "Prepare for sleep done" );
186
175
return 0 ;
187
176
}
188
177
@@ -371,16 +360,20 @@ static int parse_sleep(int argc, char **argv) {
371
360
372
361
static int parse_args (int argc , char * argv []) {
373
362
int c ;
374
- while ((c = getopt (argc , argv , "hd " )) != -1 ) {
363
+ while ((c = getopt (argc , argv , "hdw " )) != -1 ) {
375
364
switch (c ) {
376
365
case 'd' :
377
366
verbosity = LOG_DEBUG ;
378
367
break ;
368
+ case 'w' :
369
+ state .wait = true;
370
+ break ;
379
371
case 'h' :
380
372
case '?' :
381
373
printf ("Usage: %s [OPTIONS]\n" , argv [0 ]);
382
- printf (" -d\tdebug\n" );
383
374
printf (" -h\tthis help menu\n" );
375
+ printf (" -d\tdebug\n" );
376
+ printf (" -w\twait for command to finish\n" );
384
377
return 1 ;
385
378
default :
386
379
return 1 ;
0 commit comments