]> code.delx.au - pulseaudio/blobdiff - src/utils/pasuspender.c
Whitespace cleanup: Remove all multiple newlines
[pulseaudio] / src / utils / pasuspender.c
index 2172ccf558b1a9fa466d40d20f18248236ee89b5..5ede7f4c16ddbc313d8527ede9a4337f0b90b5e1 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
@@ -7,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
@@ -25,7 +23,6 @@
 #include <config.h>
 #endif
 
-#include <sys/prctl.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <limits.h>
 #include <getopt.h>
+#include <locale.h>
 
-#include <sndfile.h>
+#ifdef __linux__
+#include <sys/prctl.h>
+#endif
 
 #include <pulse/pulseaudio.h>
-#include <pulsecore/macro.h>
-
-#if PA_API_VERSION < 10
-#error Invalid PulseAudio API version
-#endif
 
-#define BUFSIZE 1024
+#include <pulsecore/i18n.h>
+#include <pulsecore/macro.h>
 
 static pa_context *context = NULL;
 static pa_mainloop_api *mainloop_api = NULL;
@@ -57,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);
 }
@@ -71,47 +66,42 @@ 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 suspend_complete(pa_context *c, int success, void *userdata) {
-    static int n = 0;
+static int start_child(void) {
 
-    n++;
-    
-    if (!success) {
-        fprintf(stderr, "Failure to suspend: %s\n", pa_strerror(pa_context_errno(c)));
-        quit(1);
-    }
+    if ((child_pid = fork()) < 0) {
+        fprintf(stderr, _("fork(): %s\n"), strerror(errno));
+        fork_failed = 1;
 
-    if (n >= 2) {
-        
-        if ((child_pid = fork()) < 0) {
+        return -1;
 
-            fprintf(stderr, "fork(): %s\n", strerror(errno));
-            quit(1);
-            
-        } else if (child_pid == 0) {
-            /* Child */
+    } else if (child_pid == 0) {
+        /* Child */
 
 #ifdef __linux__
-            prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
+        prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
 #endif
-            
-            if (execvp(child_argv[0], child_argv) < 0)
-                fprintf(stderr, "execvp(): %s\n", strerror(errno));
 
-            _exit(1);
+        if (execvp(child_argv[0], child_argv) < 0)
+            fprintf(stderr, _("execvp(): %s\n"), strerror(errno));
 
-        } else {
+        _exit(1);
 
-            /* parent */
-            dead = 0;
-        }
+    } else {
+
+        /* parent */
+        dead = 0;
     }
+
+    return 0;
 }
 
 static void resume_complete(pa_context *c, int success, void *userdata) {
@@ -120,7 +110,7 @@ static void resume_complete(pa_context *c, int success, void *userdata) {
     n++;
 
     if (!success) {
-        fprintf(stderr, "Failure to resume: %s\n", pa_strerror(pa_context_errno(c)));
+        fprintf(stderr, _("Failure to resume: %s\n"), pa_strerror(pa_context_errno(c)));
         quit(1);
         return;
     }
@@ -129,9 +119,45 @@ static void resume_complete(pa_context *c, int success, void *userdata) {
         drain(); /* drain and quit */
 }
 
+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 suspend: %s\n"), pa_strerror(pa_context_errno(c)));
+        quit(1);
+        return;
+    }
+
+    if (n >= 2) {
+        if (start_child() < 0)
+            resume();
+    }
+}
+
 static void context_state_callback(pa_context *c, void *userdata) {
     pa_assert(c);
-    
+
     switch (pa_context_get_state(c)) {
         case PA_CONTEXT_CONNECTING:
         case PA_CONTEXT_AUTHORIZING:
@@ -139,54 +165,72 @@ static void context_state_callback(pa_context *c, void *userdata) {
             break;
 
         case PA_CONTEXT_READY:
-            pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
-            pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
+            if (pa_context_is_local(c)) {
+                pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
+                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"));
+                if (start_child() < 0)
+                    drain();
+            }
+
             break;
-                    
+
         case PA_CONTEXT_TERMINATED:
             quit(0);
             break;
 
         case PA_CONTEXT_FAILED:
         default:
-            fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
-            quit(1);
+            fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
+
+            pa_context_unref(context);
+            context = NULL;
+
+            if (child_pid == (pid_t) -1) {
+                /* not started yet, then we do it now */
+                if (start_child() < 0)
+                    quit(1);
+            } else if (dead)
+                /* already started, and dead, so let's quit */
+                quit(1);
+
+            break;
     }
 }
 
 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
-    fprintf(stderr, "Got SIGINT, exiting.\n");
-    quit(0);
+    fprintf(stderr, _("Got SIGINT, exiting.\n"));
+    resume();
 }
 
 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
     int status = 0;
     pid_t p;
-    
+
     p = waitpid(-1, &status, WNOHANG);
 
     if (p != child_pid)
         return;
 
     dead = 1;
-    
+
     if (WIFEXITED(status))
         child_ret = WEXITSTATUS(status);
     else if (WIFSIGNALED(status)) {
-        fprintf(stderr, "WARNING: Child process terminated by signal %u\n", WTERMSIG(status));
+        fprintf(stderr, _("WARNING: Child process terminated by signal %u\n"), WTERMSIG(status));
         child_ret = 1;
     }
 
-    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));
+    resume();
 }
 
 static void help(const char *argv0) {
 
-    printf("%s [options] ... \n\n"
+    printf(_("%s [options] ... \n\n"
            "  -h, --help                            Show this help\n"
            "      --version                         Show version\n"
-           "  -s, --server=SERVER                   The name of the server to connect to\n\n",
+           "  -s, --server=SERVER                   The name of the server to connect to\n\n"),
            argv0);
 }
 
@@ -206,10 +250,12 @@ int main(int argc, char *argv[]) {
         {NULL,          0, NULL, 0}
     };
 
-    if (!(bn = strrchr(argv[0], '/')))
-        bn = argv[0];
-    else
-        bn++;
+    setlocale(LC_ALL, "");
+#ifdef ENABLE_NLS
+    bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
+#endif
+
+    bn = pa_path_get_filename(argv[0]);
 
     while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
         switch (c) {
@@ -219,7 +265,12 @@ int main(int argc, char *argv[]) {
                 goto quit;
 
             case ARG_VERSION:
-                printf("pasuspender "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
+                printf(_("pasuspender %s\n"
+                         "Compiled with libpulse %s\n"
+                         "Linked with libpulse %s\n"),
+                       PACKAGE_VERSION,
+                       pa_get_headers_version(),
+                       pa_get_library_version());
                 ret = 0;
                 goto quit;
 
@@ -243,7 +294,7 @@ int main(int argc, char *argv[]) {
     }
 
     if (!(m = pa_mainloop_new())) {
-        fprintf(stderr, "pa_mainloop_new() failed.\n");
+        fprintf(stderr, _("pa_mainloop_new() failed.\n"));
         goto quit;
     }
 
@@ -256,18 +307,25 @@ int main(int argc, char *argv[]) {
 #endif
 
     if (!(context = pa_context_new(mainloop_api, bn))) {
-        fprintf(stderr, "pa_context_new() failed.\n");
+        fprintf(stderr, _("pa_context_new() failed.\n"));
         goto quit;
     }
 
     pa_context_set_state_callback(context, context_state_callback, NULL);
-    pa_context_connect(context, server, 0, 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");
+        fprintf(stderr, _("pa_mainloop_run() failed.\n"));
         goto quit;
     }
 
+    if (ret == 0 && fork_failed)
+        ret = 1;
+
 quit:
     if (context)
         pa_context_unref(context);