chiark / gitweb /
Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[disorder] / disobedience / rtp.c
index a4efed5c9b42fce6c1ee95262ffb237c4d51396d..bbeec1a989be28068a7a59dd72e26613d63f6845 100644 (file)
@@ -1,31 +1,32 @@
 /*
  * This file is part of Disobedience
- * Copyright (C) 2007 Richard Kettlewell
+ * Copyright (C) 2007-2010 Richard Kettlewell
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 /** @file disobedience/rtp.c
  * @brief RTP player support for Disobedience
  */
 
 #include "disobedience.h"
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/un.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
 
 /** @brief Path to RTP player's control socket */
 static char *rtp_socket;
@@ -33,16 +34,44 @@ static char *rtp_socket;
 /** @brief Path to RTP player's logfile */
 static char *rtp_log;
 
+static char *substitute_instance_name(void) {
+  const char *p = config->rtp_instance_name;
+  struct utsname uts;
+  size_t n;
+  int ok;
+  struct dynstr z;
+
+  ok = (uname(&uts) >= 0);
+  if(!p) p = "%h-rtp";
+  dynstr_init(&z);
+  for(;;) {
+    n = strcspn(p, "%");
+    if(n) { dynstr_append_bytes(&z, p, n); p += n; }
+    if(!*p) break;
+    p++;
+    switch(*p) {
+    case '%': dynstr_append_bytes(&z, "%", 1); p++; break;
+    case 'h':
+      if(ok) { dynstr_append_string(&z, uts.nodename); p++; }
+      else { p++; if(*p) p++; }
+    case 0: break;
+    default: dynstr_append_bytes(&z, p - 1, 2); p++; break;
+    }
+  }
+  dynstr_terminate(&z);
+  return z.vec;
+}
+
 /** @brief Initialize @ref rtp_socket and @ref rtp_log if necessary */
 static void rtp_init(void) {
   if(!rtp_socket) {
-    const char *home = getenv("HOME");
-    char *dir;
-
-    byte_xasprintf(&dir, "%s/.disorder", home);
+    const char *dir, *instance;
+    if(!(dir = profile_directory()))
+      disorder_fatal(0, "failed to find profile directory");
     mkdir(dir, 02700);
-    byte_xasprintf(&rtp_socket, "%s/rtp", dir);
-    byte_xasprintf(&rtp_log, "%s/rtp.log", dir);
+    instance = substitute_instance_name();
+    byte_xasprintf(&rtp_socket, "%s/%s", dir, instance);
+    byte_xasprintf(&rtp_log, "%s/%s.log", dir, instance);
   }
 }
 
@@ -91,6 +120,27 @@ int rtp_running(void) {
   return 1;
 }
 
+int rtp_getvol(int *l, int *r) {
+  FILE *fp;
+  int rc = -1;
+
+  fp = rtp_connect(); if(!fp) goto end;
+  fprintf(fp, "getvol\n"); fflush(fp);
+  if(fscanf(fp, "%d %d\n", l, r) != 2) goto end;
+  rc = 0;
+end:
+  if(fp) fclose(fp);
+  return rc;
+}
+
+int rtp_setvol(int *l, int *r) {
+  FILE *fp = rtp_connect(); if(!fp) return -1;
+  fprintf(fp, "setvol %d %d\n", *l, *r); fflush(fp);
+  if(fscanf(fp, "%d %d\n", l, r) != 2) { /* do nothing */ }
+  fclose(fp);
+  return 0;
+}
+
 /** @brief Activate the RTP player if it is not running */
 void start_rtp(void) {
   pid_t pid;
@@ -101,30 +151,35 @@ void start_rtp(void) {
   /* double-fork so we don't have to wait() later */
   if(!(pid = xfork())) {
     if(setsid() < 0)
-      fatal(errno, "error calling setsid");
-    if(!(pid = xfork())) {
+      disorder_fatal(errno, "error calling setsid");
+    if(!xfork()) {
       /* grandchild */
       exitfn = _exit;
       /* log errors and output somewhere reasonably sane.  rtp_running()
        * will have made sure the directory exists. */
       if((fd = open(rtp_log, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
-       fatal(errno, "creating %s", rtp_log);
+       disorder_fatal(errno, "creating %s", rtp_log);
       if(dup2(fd, 1) < 0
         || dup2(fd, 2) < 0)
-       fatal(errno, "dup2");
+       disorder_fatal(errno, "dup2");
       if(close(fd) < 0)
-       fatal(errno, "close");
+       disorder_fatal(errno, "close");
       /* We don't want to hang onto whatever stdin was */
       if((fd = open("/dev/null", O_RDONLY)) < 0)
-        fatal(errno, "opening /dev/null");
+        disorder_fatal(errno, "opening /dev/null");
       if(dup2(fd, 0) < 0)
-        fatal(errno, "dup2");
+        disorder_fatal(errno, "dup2");
       if(close(fd) < 0)
-       fatal(errno, "close");
+       disorder_fatal(errno, "close");
       /* execute the player */
       execlp("disorder-playrtp",
-            "disorder-playrtp", "--socket", rtp_socket, (char *)0);
-      fatal(errno, "disorder-playrtp");
+            "disorder-playrtp",
+             "--socket", rtp_socket,
+             "--api", rtp_api,
+             "--config", configfile,
+             "--user-config", userconfigfile,
+             (char *)0);
+      disorder_fatal(errno, "disorder-playrtp");
     } else {
       /* child */
       _exit(0);
@@ -146,6 +201,74 @@ void stop_rtp(void) {
   fclose(fp);
 }
 
+static char *rtp_config_file(void) {
+  static char *rtp_config;
+  if(!rtp_config)
+    rtp_config = profile_filename("api");
+  return rtp_config;
+}
+
+const char *rtp_api;
+
+void load_rtp_config(void) {
+  char *rtp_config = rtp_config_file();
+  FILE *fp;
+  if((fp = fopen(rtp_config, "r"))) {
+    char *line;
+    if(inputline(rtp_config, fp, &line, '\n') == 0) {
+      for(int n = 0; uaudio_apis[n]; ++n)
+        if(!strcmp(uaudio_apis[n]->name, line))
+          rtp_api = line;
+    }
+    fclose(fp);
+  }
+  if(!rtp_api)
+    rtp_api = uaudio_default(uaudio_apis, UAUDIO_API_CLIENT)->name;
+}
+
+void save_rtp_config(void) {
+  if(rtp_api) {
+    char *rtp_config = rtp_config_file();
+    char *tmp;
+    byte_xasprintf(&tmp, "%s.tmp", rtp_config);
+    FILE *fp;
+    if(!(fp = fopen(tmp, "w"))){
+      fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
+                 tmp, strerror(errno));
+      return;
+    }
+    if(fprintf(fp, "%s\n", rtp_api) < 0) {
+      fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
+                 tmp, strerror(errno));
+      fclose(fp);
+      return;
+    }
+    if(fclose(fp) < 0) {
+      fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s",
+                 tmp, strerror(errno));
+      return;
+    }
+    if(rename(tmp, rtp_config) < 0) {
+      fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s",
+                 tmp, strerror(errno));
+    }
+  }
+}
+
+void change_rtp_api(const char *api) {
+  if(rtp_api && !strcmp(api, rtp_api))
+    return;                             /* no change */
+  int running = rtp_running();
+  if(running)
+    stop_rtp();
+  rtp_api = api;
+  save_rtp_config();
+  // TODO this is racy and does not work; the player doesn't shut down quickly
+  // enough.
+  if(running)
+    start_rtp();
+}
+
 /*
 Local Variables:
 c-basic-offset:2