chiark / gitweb /
Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[disorder] / disobedience / rtp.c
index d88c3ec062f76f5e8ad095dbe293c80684219b78..bbeec1a989be28068a7a59dd72e26613d63f6845 100644 (file)
@@ -34,21 +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, *base;
-    struct utsname uts;
-
-    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);
-    if(uname(&uts) < 0)
-      byte_xasprintf(&base, "%s/", dir);
-    else
-      byte_xasprintf(&base, "%s/%s-", dir, uts.nodename);
-    byte_xasprintf(&rtp_socket, "%srtp", base);
-    byte_xasprintf(&rtp_log, "%srtp.log", base);
+    instance = substitute_instance_name();
+    byte_xasprintf(&rtp_socket, "%s/%s", dir, instance);
+    byte_xasprintf(&rtp_log, "%s/%s.log", dir, instance);
   }
 }
 
@@ -97,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;
@@ -132,6 +176,8 @@ void start_rtp(void) {
             "disorder-playrtp",
              "--socket", rtp_socket,
              "--api", rtp_api,
+             "--config", configfile,
+             "--user-config", userconfigfile,
              (char *)0);
       disorder_fatal(errno, "disorder-playrtp");
     } else {
@@ -157,9 +203,8 @@ void stop_rtp(void) {
 
 static char *rtp_config_file(void) {
   static char *rtp_config;
-  const char *home = getenv("HOME");
   if(!rtp_config)
-    byte_xasprintf(&rtp_config, "%s/.disorder/api", home);
+    rtp_config = profile_filename("api");
   return rtp_config;
 }