]> code.delx.au - pulseaudio/blobdiff - src/utils/pasuspender.c
Whitespace cleanup: Remove all multiple newlines
[pulseaudio] / src / utils / pasuspender.c
index 8a59d5e452981b5c2b63983693f76a7801744e59..5ede7f4c16ddbc313d8527ede9a4337f0b90b5e1 100644 (file)
@@ -5,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
@@ -33,7 +33,6 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <limits.h>
 #include <getopt.h>
 #include <locale.h>
 
 #include <sys/prctl.h>
 #endif
 
-#include <pulse/i18n.h>
 #include <pulse/pulseaudio.h>
-#include <pulsecore/macro.h>
 
-#define BUFSIZE 1024
+#include <pulsecore/i18n.h>
+#include <pulsecore/macro.h>
 
 static pa_context *context = NULL;
 static pa_mainloop_api *mainloop_api = NULL;
@@ -54,13 +52,13 @@ static int child_argc = 0;
 static pid_t child_pid = (pid_t) -1;
 static int child_ret = 0;
 static int dead = 1;
+static int fork_failed = 0;
 
 static void quit(int ret) {
     pa_assert(mainloop_api);
     mainloop_api->quit(mainloop_api, ret);
 }
 
-
 static void context_drain_complete(pa_context *c, void *userdata) {
     pa_context_disconnect(c);
 }
@@ -68,18 +66,22 @@ static void context_drain_complete(pa_context *c, void *userdata) {
 static void drain(void) {
     pa_operation *o;
 
-    if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
-        pa_context_disconnect(context);
-    else
-        pa_operation_unref(o);
+    if (context) {
+        if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
+            pa_context_disconnect(context);
+        else
+            pa_operation_unref(o);
+    } else
+        quit(0);
 }
 
-static void start_child(void) {
+static int start_child(void) {
 
     if ((child_pid = fork()) < 0) {
-
         fprintf(stderr, _("fork(): %s\n"), strerror(errno));
-        quit(1);
+        fork_failed = 1;
+
+        return -1;
 
     } else if (child_pid == 0) {
         /* Child */
@@ -98,36 +100,59 @@ static void start_child(void) {
         /* parent */
         dead = 0;
     }
+
+    return 0;
 }
 
-static void suspend_complete(pa_context *c, int success, void *userdata) {
+static void resume_complete(pa_context *c, int success, void *userdata) {
     static int n = 0;
 
     n++;
 
     if (!success) {
-        fprintf(stderr, _("Failure to suspend: %s\n"), pa_strerror(pa_context_errno(c)));
+        fprintf(stderr, _("Failure to resume: %s\n"), pa_strerror(pa_context_errno(c)));
         quit(1);
         return;
     }
 
     if (n >= 2)
-        start_child();
+        drain(); /* drain and quit */
 }
 
-static void resume_complete(pa_context *c, int success, void *userdata) {
+static void resume(void) {
+    static int n = 0;
+
+    n++;
+
+    if (n > 1)
+        return;
+
+    if (context) {
+        if (pa_context_is_local(context)) {
+            pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
+            pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
+        } else
+            drain();
+    } else {
+        quit(0);
+    }
+}
+
+static void suspend_complete(pa_context *c, int success, void *userdata) {
     static int n = 0;
 
     n++;
 
     if (!success) {
-        fprintf(stderr, _("Failure to resume: %s\n"), pa_strerror(pa_context_errno(c)));
+        fprintf(stderr, _("Failure to suspend: %s\n"), pa_strerror(pa_context_errno(c)));
         quit(1);
         return;
     }
 
-    if (n >= 2)
-        drain(); /* drain and quit */
+    if (n >= 2) {
+        if (start_child() < 0)
+            resume();
+    }
 }
 
 static void context_state_callback(pa_context *c, void *userdata) {
@@ -145,7 +170,8 @@ static void context_state_callback(pa_context *c, void *userdata) {
                 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
             } else {
                 fprintf(stderr, _("WARNING: Sound server is not local, not suspending.\n"));
-                start_child();
+                if (start_child() < 0)
+                    drain();
             }
 
             break;
@@ -161,10 +187,11 @@ static void context_state_callback(pa_context *c, void *userdata) {
             pa_context_unref(context);
             context = NULL;
 
-            if (child_pid == (pid_t) -1)
+            if (child_pid == (pid_t) -1) {
                 /* not started yet, then we do it now */
-                start_child();
-            else if (dead)
+                if (start_child() < 0)
+                    quit(1);
+            } else if (dead)
                 /* already started, and dead, so let's quit */
                 quit(1);
 
@@ -174,7 +201,7 @@ static void context_state_callback(pa_context *c, void *userdata) {
 
 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
     fprintf(stderr, _("Got SIGINT, exiting.\n"));
-    quit(0);
+    resume();
 }
 
 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
@@ -195,16 +222,7 @@ static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, vo
         child_ret = 1;
     }
 
-    if (context) {
-        if (pa_context_is_local(context)) {
-            /* A context is around, so let's resume */
-            pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
-            pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
-        } else
-            drain();
-    } else
-        /* Hmm, no context here, so let's terminate right away */
-        quit(0);
+    resume();
 }
 
 static void help(const char *argv0) {
@@ -233,12 +251,11 @@ int main(int argc, char *argv[]) {
     };
 
     setlocale(LC_ALL, "");
+#ifdef ENABLE_NLS
     bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
+#endif
 
-    if (!(bn = strrchr(argv[0], '/')))
-        bn = argv[0];
-    else
-        bn++;
+    bn = pa_path_get_filename(argv[0]);
 
     while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
         switch (c) {
@@ -295,13 +312,20 @@ int main(int argc, char *argv[]) {
     }
 
     pa_context_set_state_callback(context, context_state_callback, NULL);
-    pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL);
+
+    if (pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
+        fprintf(stderr, "pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(context)));
+        goto quit;
+    }
 
     if (pa_mainloop_run(m, &ret) < 0) {
         fprintf(stderr, _("pa_mainloop_run() failed.\n"));
         goto quit;
     }
 
+    if (ret == 0 && fork_failed)
+        ret = 1;
+
 quit:
     if (context)
         pa_context_unref(context);