source: roaraudio/roard/roard.c @ 3907:3146f952e764

Last change on this file since 3907:3146f952e764 was 3907:3146f952e764, checked in by phi, 14 years ago

only declare vars we use

File size: 50.6 KB
Line 
1//roard.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifdef ROAR_SUPPORT_LISTEN
29char * server[ROAR_MAX_LISTEN_SOCKETS];
30#endif
31
32#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
33#define SUPPORT_PIDFILE
34char * pidfile = NULL;
35#endif
36
37#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
38int    setids    = 0;
39#endif
40
41#ifdef ROAR_HAVE_LIBX11
42char * x11display = NULL;
43#endif
44
45#ifdef ROAR_HAVE_MAIN_ARGS
46void usage (void) {
47 printf("Usage: roard [OPTIONS]...\n\n");
48
49 printf("Misc Options:\n\n");
50 printf(
51        " --daemon              - Bring the server into background after init\n"
52        " --verbose             - Be more verbose, can be used multiple times\n"
53        " --terminate           - Terminate after last client quited\n"
54        " --start               - No op parameter (starting roard is default operation)\n"
55        " --restart             - Trys to stop an old instance and start a new with new settings\n"
56        " --stop                - Stops a running roard (provide --pidfile!)\n"
57        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
58        " --realtime            - Trys to get realtime priority,\n"
59        "                         give multible times for being more realtime\n"
60        " --chroot DIR          - chroots to the given dir\n"
61        " --setgid              - GroupID to the audio group as specified via -G\n"
62        " --setuid              - UserID to the audio user as specified via -U\n"
63        " --sysclocksync        - calculate exact sample rate using the system clock\n"
64        " --location  LOC       - Set lion readable location of server\n"
65#ifdef SUPPORT_PIDFILE
66        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
67#endif
68#ifdef ROAR_HAVE_SYSLOG
69        " --log-syslog          - Log Warnings, Errors, ... to syslog\n"
70#endif
71       );
72
73 printf("\nPlugin Options:\n\n");
74 printf(
75        " --plugin-load FILE    - Load plugin FILE\n"
76       );
77
78 printf("\nAudio Options:\n\n");
79 printf(
80        " -R  --rate   RATE     - Set server rate\n"
81        " -B  --bits   BITS     - Set server bits\n"
82        " -C  --chans  CHANNELS - Set server channels\n"
83       );
84
85 printf("\nStream Options:\n\n");
86 printf(
87        " --stream-flags D=F    - Set default flags for stream directions\n"
88        "                         D is the stream direction and F is a comma seperated\n"
89        "                         list of flags in form +flag or -flag to set or unset\n"
90        "                         a flag as default or remove it from the default\n"
91       );
92
93 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
94 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
95 printf(" -D  --device DEV      - Set the device\n");
96 printf(" -dO OPTS              - Set output options\n");
97 printf(" --list-driver         - List all drivers\n");
98
99 printf("\nOutput Options:\n\n");
100 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
101 printf(" -O  --odevice DEV     - Set the device\n");
102 printf(" -oO OPTS              - Set output options\n");
103 printf(" -oN                   - Adds another output\n");
104 printf(" -oP                   - Mark output as primary\n");
105
106#ifndef ROAR_WITHOUT_DCOMP_SOURCES
107 printf("\nSource Options:\n\n");
108 printf(" -s  --source DRV      - Use DRV as input driver\n"
109        " -S           DEV      - Use DEV as input device\n"
110        " -sO          OPTS     - Use OPTS as input options\n"
111        " -sN                   - Adds another source\n"
112        " -sP                   - Make souce as primary\n"
113       );
114 printf(" --list-sources        - List all sources\n");
115#endif
116
117 printf("\nCodec Filter Options:\n\n");
118 printf(" --list-cf             - List all codec filter\n"
119       );
120
121#ifndef ROAR_WITHOUT_DCOMP_MIDI
122 printf("\nMIDI Options:\n\n");
123 printf(" --midi-no-console     - Disable console based MIDI synth\n"
124        " --midi-console-enable - Enables the console based MIDI synth\n"
125        " --midi-console DEV    - Set device for MIDI console\n"
126#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
127        " --ssynth-enable       - Enable simple software synth\n"
128        " --ssynth-disable      - Disable simple software synth\n"
129#endif
130       );
131#endif
132
133#ifndef ROAR_WITHOUT_DCOMP_LIGHT
134 printf("\nLight Control Options:\n\n");
135 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
136                                  LIGHT_CHANNELS_DEFAULT
137       );
138#endif
139
140#ifndef ROAR_WITHOUT_DCOMP_RDTCS
141 printf("\nRadio Data and Transmitter Control System Options:\n\n");
142 printf(" --rds-pi   PI         - Sets the RDS Programme Identification (PI)\n"
143        " --rds-ps   PS         - Sets the RDS Programme Service Name (PS)\n"
144        " --rds-pty  PTY        - Sets the RDS Programme Type (PTY)\n"
145        " --rds-tp              - Sets the RDS Traffic Programme (TP) flag\n"
146        " --rds-ct              - Enables sending of RDS Clock Time (CT)\n"
147       );
148#endif
149
150#ifdef ROAR_HAVE_LIBX11
151 printf("\nX11 Options:\n\n");
152 printf(
153        " --x11-display DISPLAY - Set display for X11\n"
154        " --display DISPLAY     - Set display for X11\n"
155       );
156#endif
157
158 printf("\nServer Options:\n\n");
159 printf(" -t  --tcp             - Use TCP listen socket\n"
160        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
161#ifdef ROAR_HAVE_LIBDNET
162        " -n  --decnet          - use DECnet listen socket\n"
163#endif
164        " -4                    - Use IPv4 connections (implies -t)\n"
165#ifdef AF_INET6
166        " -6                    - Use IPv6 connections (implies -t)\n"
167#endif
168#ifdef IPV6_ADDRFORM
169        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
170        "                         this is normaly not usefull.\n"
171#endif
172        " -p  --port            - TCP Port to bind to\n"
173        " -b  --bind            - IP/Hostname to bind to\n"
174        "     --sock            - Filename for UNIX Domain Socket\n"
175        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
176        "     --proto-dir DIR   - Set direction parameter for protocol\n"
177        "     --proto-rate RATE - Set sample rate parameter for protocol\n"
178        "     --proto-bits BITS - Set bits per sample parameter for protocol\n"
179        "     --proto-codec E   - Set codec parameter for protocol\n"
180        "     --proto-chans C   - Set number of channels paramter for protocol\n"
181        "     --list-proto      - List supported protocols\n"
182        "     --new-sock        - Parameters for new socket follows\n"
183#ifdef ROAR_HAVE_LIBSLP
184        "     --slp             - Enable OpenSLP support\n"
185#endif
186#ifdef ROAR_HAVE_LIBX11
187        "     --x11             - Enable X11 support\n"
188#endif
189        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
190        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
191        "                         You need the permissions to change the GID\n"
192        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
193        "                         You need the permissions to change the UID (normaly only root has)\n"
194        " --no-listen           - Do not listen for new clients\n"
195        "                         (only usefull for relaing, impleys --terminate)\n"
196        " --client-fh           - Comunicate with a client over this handle\n"
197        "                         (only usefull for relaing)\n"
198        " --close-fh            - Closes the given fh\n"
199        " --standby             - Start in standby state\n"
200        " --auto-standby        - Automatical goes into standby if there are no streams\n",
201#ifdef ROAR_DEFAULT_SOCKGRP
202        ROAR_DEFAULT_SOCKGRP
203#else
204        "(none)"
205#endif
206       );
207// printf("\n Options:\n\n");
208 printf("\n");
209}
210
211static void list_proto (void) {
212 printf("  Protocol Flag Subsys - Description\n");
213 printf("------------------------------------------------------\n");
214 printf("  roar          WM LRX - RoarAudio native protocol\n");
215#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
216 printf("  esd           W      - EsounD emulation\n");
217#endif
218#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
219 printf("  simple        WM LRX - PulseAudio simple protocol\n");
220#endif
221#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
222 printf("  rsound        W      - RSound emulation\n");
223#endif
224}
225
226#endif
227
228int restart_server (char * server, int terminate) {
229 struct roar_connection con;
230#ifdef ROAR_HAVE_KILL
231 char buf[80];
232 ssize_t l;
233 struct roar_vio_calls fh;
234 pid_t pid;
235 int ok;
236
237 if ( pidfile != NULL ) {
238  if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) {
239   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
240  } else {
241   l = roar_vio_read(&fh, buf, 80);
242   roar_vio_close(&fh);
243   if ( l > 0 ) {
244    buf[l-1] = 0;
245    buf[79]  = 0;
246    pid = atoi(buf);
247    if ( terminate ) {
248     ok = kill(pid, SIGUSR1);
249    } else {
250     ok = kill(pid, SIGINT);
251    }
252    if ( ok == 0 ) {
253     return 0;
254    } else {
255     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
256    }
257   } else {
258    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
259   }
260  }
261 }
262#endif
263
264 if ( roar_connect(&con, server) == -1 ) {
265  return -1;
266 }
267
268 if ( roar_terminate(&con, terminate) == -1 ) {
269  return -1;
270 }
271
272 return roar_disconnect(&con);
273}
274
275#define R_SETUID 1
276#define R_SETGID 2
277
278int init_config (void) {
279 int i;
280
281 memset(g_config, 0, sizeof(struct roard_config));
282
283 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
284  g_config->streams[i].mixer_channels = 1;
285  g_config->streams[i].mixer.rpg_mul  = 1;
286  g_config->streams[i].mixer.rpg_div  = 1;
287  g_config->streams[i].mixer.scale    = 65535;
288  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
289 }
290
291 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
292 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
293 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
294 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
295 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
296
297 g_config->location = "***default***";
298
299 return 0;
300}
301
302#ifdef ROAR_SUPPORT_LISTEN
303int init_listening (void) {
304 int i;
305
306 memset(g_listen, 0, sizeof(g_listen));
307
308 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
309  g_listen[i].proto  = ROAR_PROTO_ROARAUDIO;
310  server[i]          = NULL;
311 }
312
313 return 0;
314}
315
316int get_listen(struct roard_listen ** sock, char *** sockname) {
317 int i;
318
319 if ( sock == NULL )
320  return -1;
321
322 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
323  if ( ! g_listen[i].used ) {
324   server[i] = NULL;
325   *sock = &(g_listen[i]);
326
327   if ( sockname != NULL )
328    *sockname = &(server[i]);
329
330   return 0;
331  }
332 }
333
334 return -1;
335}
336
337int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
338#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
339 struct group   * grp  = NULL;
340#endif
341#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
342 struct passwd  * pwd  = NULL;
343#endif
344#ifdef ROAR_HAVE_UNIX
345 char * env_roar_proxy_backup;
346#endif
347 int    sockid = -1;
348#ifdef ROAR_HAVE_UNIX
349 int    sock;
350#endif
351 int    i;
352
353 if ( *addr != 0 ) {
354  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
355   if ( ! g_listen[i].used ) {
356    sockid = i;
357    break;
358   }
359  }
360
361  if ( sockid == -1 )
362   return -1;
363
364  g_listen[sockid].proto = proto;
365
366  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
367
368  if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
369#ifdef ROAR_HAVE_UNIX
370   if ( *addr == '/' ) {
371    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
372     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
373     unsetenv("ROAR_PROXY");
374    }
375    if ( (sock = roar_socket_connect(addr, port)) != -1 ) {
376     close(sock);
377     ROAR_ERR("Can not open listen socket: Socket allready in use");
378     return 1;
379    } else {
380     unlink(addr);
381     if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
382      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
383      return 1;
384     }
385    }
386    if ( env_roar_proxy_backup != NULL ) {
387     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
388     free(env_roar_proxy_backup);
389    }
390#else
391   if (0) { // noop
392#endif
393   } else {
394    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
395    return 1;
396   }
397  }
398
399#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
400  if ( group != NULL ) {
401   if ( (grp = getgrnam(group)) == NULL ) {
402    ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
403   }
404  }
405#endif
406#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
407  if ( user ) {
408   if ( (pwd = getpwnam(user)) == NULL ) {
409    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
410   }
411  }
412#endif
413
414#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
415  if ( *addr == '/' ) {
416   if ( grp || pwd ) {
417     if ( chown(addr, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 )
418      return 1;
419   }
420#ifdef ROAR_HAVE_GETUID
421   if ( grp ) {
422    if ( getuid() == 0 )
423     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
424      return 1;
425   }
426#endif
427  }
428#endif
429 }
430
431 // in case we opened the listening socket correctly.
432 if ( dir == -1 )
433  dir = ROAR_DIR_PLAY;
434
435 g_listen[sockid].inst.stpl.dir = dir;
436 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
437
438 switch (dir) {
439  case ROAR_DIR_PLAY:
440  case ROAR_DIR_RECORD:
441  case ROAR_DIR_MONITOR:
442  case ROAR_DIR_FILTER:
443  case ROAR_DIR_BIDIR:
444    if ( !g_listen[sockid].inst.stpl.info.rate )
445     g_listen[sockid].inst.stpl.info.rate = g_sa->rate;
446
447    if ( !g_listen[sockid].inst.stpl.info.bits )
448     g_listen[sockid].inst.stpl.info.bits = g_sa->bits;
449
450    if ( !g_listen[sockid].inst.stpl.info.channels )
451     g_listen[sockid].inst.stpl.info.channels = g_sa->channels;
452
453    if ( !g_listen[sockid].inst.stpl.info.codec )
454     g_listen[sockid].inst.stpl.info.codec = g_sa->codec;
455   break;
456 }
457
458 g_listen[sockid].used = 1;
459 server[sockid]        = addr;
460
461 return 0;
462}
463#endif
464
465int update_stream_flags (char * str) {
466 int    dir;
467 char * flags;
468 char * k;
469 int    op;
470 int    flag;
471
472 if ( (flags = strstr(str, "=")) == NULL )
473  return -1;
474
475 *flags = 0;
476  flags++;
477
478 if ( (dir = roar_str2dir(str)) == -1 )
479  return -1;
480
481 while (flags != NULL) {
482  k = flags;
483  flags = strstr(flags, ",");
484
485  if ( flags != NULL )
486   *(flags++) = 0;
487
488  switch (*k) {
489   case '+': k++; op = ROAR_SET_FLAG;   break;
490   case '-': k++; op = ROAR_RESET_FLAG; break;
491   default:
492     op = ROAR_SET_FLAG;
493  }
494
495  flag = 0;
496
497  if ( !strcmp(k, "sync") ) {
498   flag = ROAR_FLAG_SYNC;
499  } else if ( !strcmp(k, "meta") ) {
500   flag = ROAR_FLAG_META;
501  } else if ( !strcmp(k, "cleanmeta") ) {
502   flag = ROAR_FLAG_CLEANMETA;
503  } else if ( !strcmp(k, "pause") ) {
504   flag = ROAR_FLAG_PAUSE;
505  } else if ( !strcmp(k, "mute") ) {
506   flag = ROAR_FLAG_MUTE;
507  } else if ( !strcmp(k, "antiecho") ) {
508   flag = ROAR_FLAG_ANTIECHO;
509  } else if ( !strcmp(k, "passmixer") ) {
510   flag = ROAR_FLAG_PASSMIXER;
511  } else {
512   return -1;
513  }
514
515  g_config->streams[dir].flags |= flag;
516
517  if ( op == ROAR_RESET_FLAG )
518   g_config->streams[dir].flags -= flag;
519 }
520
521 return 0;
522}
523
524int add_output (char * drv, char * dev, char * opts, int prim, int count) {
525 int stream;
526 struct roar_stream * s;
527 struct roar_stream_server * ss;
528 char * k, * v;
529#ifdef ROAR_DRIVER_CODEC
530 char * to_free = NULL;
531#endif
532 int sync = 0, f_mmap = 0;
533 int32_t blocks = -1, blocksize = -1;
534 int dir = ROAR_DIR_OUTPUT;
535 int error = 0;
536 // DMX:
537 int32_t channel  = -1;
538 int32_t universe = -1;
539 uint16_t tu16;
540 float q = -32e6;
541
542 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
543
544 if ( drv == NULL && count == 0 ) {
545  drv  = ROAR_DRIVER_DEFAULT;
546  prim = 1;
547  sync = 1;
548
549#ifdef ROAR_DRIVER_CODEC
550  if ( opts == NULL ) {
551   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
552  }
553#endif
554 }
555
556 if ( opts == NULL && count == 0 ) {
557  sync = 1;
558  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
559 }
560
561 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
562
563 if ( (stream = streams_new()) == -1 ) {
564  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
565  if ( prim ) alive = 0;
566  return -1;
567 }
568
569 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
570
571 streams_get(stream, &ss);
572 s = ROAR_STREAM(ss);
573
574 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
575
576 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
577
578 s->pos_rel_id = -1;
579// s->info.codec = codec;
580
581 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
582
583 if ( opts == NULL ) {
584  k = NULL;
585 } else {
586  k = strtok(opts, ",");
587 }
588
589 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
590
591 while (k != NULL) {
592//  ROAR_WARN("add_output(*): opts: %s", k);
593
594  if ( (v = strstr(k, "=")) != NULL ) {
595   *v++ = 0;
596  }
597
598  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
599  if ( strcmp(k, "rate") == 0 ) {
600   s->info.rate = atoi(v);
601  } else if ( strcmp(k, "channels") == 0 ) {
602   s->info.channels = atoi(v);
603  } else if ( strcmp(k, "bits") == 0 ) {
604   s->info.bits = atoi(v);
605  } else if ( strcmp(k, "codec") == 0 ) {
606   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
607    ROAR_ERR("add_output(*): unknown codec '%s'", v);
608    error++;
609   }
610  } else if ( strcmp(k, "q") == 0 ) {
611   q = atof(v);
612  } else if ( strcmp(k, "blocks") == 0 ) {
613   blocks = atoi(v);
614  } else if ( strcmp(k, "blocksize") == 0 ) {
615   blocksize = atoi(v);
616  } else if ( strcmp(k, "mmap") == 0 ) {
617   f_mmap = 1;
618  } else if ( strcmp(k, "subsystem") == 0 ) {
619   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
620    dir = ROAR_DIR_OUTPUT;
621#ifndef ROAR_WITHOUT_DCOMP_MIDI
622   } else if ( !strcasecmp(v, "midi") ) {
623    dir = ROAR_DIR_MIDI_OUT;
624#endif
625#ifndef ROAR_WITHOUT_DCOMP_LIGHT
626   } else if ( !strcasecmp(v, "light") ) {
627    dir = ROAR_DIR_LIGHT_OUT;
628#endif
629#ifndef ROAR_WITHOUT_DCOMP_RAW
630   } else if ( !strcasecmp(v, "raw") ) {
631    dir = ROAR_DIR_RAW_OUT;
632#endif
633   } else if ( !strcasecmp(v, "complex") ) {
634    dir = ROAR_DIR_COMPLEX_OUT;
635   } else {
636    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
637    error++;
638   }
639  // DMX:
640  } else if ( strcmp(k, "channel") == 0 ) {
641   channel  = atoi(v);
642   if ( channel < 0 || channel > 65535 ) {
643    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
644    channel = -1;
645    error++;
646   }
647  } else if ( strcmp(k, "universe") == 0 ) {
648   universe = atoi(v);
649   if ( universe < 0 || universe > 255 ) {
650    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
651    universe = -1;
652    error++;
653   }
654
655  } else if ( strcmp(k, "name") == 0 ) {
656   if ( streams_set_name(stream, v) == -1 ) {
657    ROAR_ERR("add_output(*): Can not set Stream name");
658    error++;
659   }
660
661  } else if ( strcmp(k, "meta") == 0 ) {
662   streams_set_flag(stream, ROAR_FLAG_META);
663  } else if ( strcmp(k, "sync") == 0 ) {
664   sync = 1;
665  } else if ( strcmp(k, "primary") == 0 ) {
666   prim = 1;
667
668  } else if ( strcmp(k, "cleanmeta") == 0 ) {
669   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
670  } else if ( strcmp(k, "autoconf") == 0 ) {
671   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
672  } else if ( strcmp(k, "recsource") == 0 ) {
673   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
674  } else if ( strcmp(k, "passmixer") == 0 ) {
675   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
676  } else {
677   ROAR_ERR("add_output(*): unknown option '%s'", k);
678   error++;
679  }
680
681  if ( error ) {
682   streams_delete(stream);
683   if ( prim ) alive = 0;
684#ifdef ROAR_DRIVER_CODEC
685   if ( to_free != NULL )
686    free(to_free);
687#endif
688   return -1;
689  }
690
691  k = strtok(NULL, ",");
692 }
693
694 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
695
696 // set audio info...
697 switch (dir) {
698  case ROAR_DIR_LIGHT_OUT:
699    switch (s->info.codec) {
700     case ROAR_CODEC_DMX512:
701     case -1:
702       if ( s->info.rate == -1 )
703        s->info.rate = ROAR_OUTPUT_CFREQ;
704
705       s->info.channels =   0;
706       s->info.bits     =   8;
707       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
708      break;
709    }
710   break;
711  case ROAR_DIR_MIDI_OUT:
712    switch (s->info.codec) {
713     case ROAR_CODEC_MIDI:
714     case -1:
715       if ( s->info.rate == -1 )
716        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
717
718       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
719       s->info.bits     = ROAR_MIDI_BITS;
720       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
721      break;
722    }
723   break;
724  case ROAR_DIR_RAW_OUT:
725    if ( s->info.rate == -1 )
726     s->info.rate = 0;
727    if ( s->info.bits == -1 )
728     s->info.bits = 0;
729    if ( s->info.channels == -1 )
730     s->info.channels = 0;
731    if ( s->info.codec == -1 )
732     s->info.codec = 0;
733   break;
734 }
735
736 if ( s->info.rate == -1 )
737  s->info.rate = g_sa->rate;
738 if ( s->info.bits == -1 )
739  s->info.bits = g_sa->bits;
740 if ( s->info.channels == -1 )
741  s->info.channels = g_sa->channels;
742 if ( s->info.codec == -1 )
743  s->info.codec = g_sa->codec;
744
745 ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec);
746
747 if ( streams_set_dir(stream, dir, 1) == -1 ) {
748  streams_delete(stream);
749  return -1;
750 }
751
752#ifdef ROAR_DRIVER_CODEC
753 if ( to_free != NULL )
754  free(to_free);
755#endif
756
757 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
758  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
759
760 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
761
762 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
763  ss->driver_id = -1; // don't close a driver not opened...
764  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
765  streams_delete(stream);
766  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
767  if ( prim ) alive = 0;
768  return -1;
769 }
770
771 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
772 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
773
774 if ( blocks != -1 )
775  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
776
777 if ( blocksize != -1 )
778  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
779
780 // TODO: we shoudld *really* check for errors here...
781 if ( channel != -1 ) {
782  tu16 = channel;
783  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
784 }
785 if ( universe != -1 ) {
786  tu16 = universe;
787  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
788 }
789
790 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
791
792 streams_set_fh(stream, -1); // update some internal structures
793
794 if ( q > -1e6 ) {
795  ROAR_DBG("add_output(*): setting q=%f", q);
796  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
797 }
798
799 client_stream_add(g_self_client, stream);
800
801 if ( prim ) {
802  streams_mark_primary(stream);
803  s->pos_rel_id = stream;
804 }
805
806 if ( sync ) {
807  streams_set_flag(stream, ROAR_FLAG_SYNC);
808 } else {
809  streams_reset_flag(stream, ROAR_FLAG_SYNC);
810 }
811
812 if ( f_mmap )
813  streams_set_flag(stream, ROAR_FLAG_MMAP);
814
815 ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec);
816 return 0;
817}
818
819// X11:
820#ifdef ROAR_HAVE_LIBX11
821int register_x11 (int unreg, char * sockname) {
822 struct roar_x11_connection * x11con = NULL;
823 int ret = 0;
824
825 if ( (x11con = roar_x11_connect(x11display)) == NULL )
826  return -1;
827
828 if ( unreg ) {
829  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 )
830   ret = -1;
831 } else {
832  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 )
833   ret = -1;
834 }
835
836 roar_x11_disconnect(x11con);
837
838 return ret;
839}
840#endif
841
842// SLP:
843void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
844 /* return the error code in the cookie */
845 *(SLPError*)cookie = errcode;
846}
847
848int register_slp (int unreg, char * sockname) {
849#ifdef ROAR_HAVE_LIBSLP
850 static int regged = 0;
851 static char * sn = NULL;
852 SLPError err;
853 SLPError callbackerr;
854 SLPHandle hslp;
855 char addr[1024];
856 char attr[1024] = "";
857 char * location;
858
859 if ( sockname != NULL )
860  sn = sockname;
861
862 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
863
864 err = SLPOpen("en", SLP_FALSE, &hslp);
865
866 if (err != SLP_OK) {
867  ROAR_ERR("Error opening slp handle: Error #%i", err);
868  return -1;
869 }
870
871 if (!unreg) {
872
873  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
874   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
875   SLPClose(hslp);
876   return -1;
877  }
878
879  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
880#ifndef ROAR_WITHOUT_DCOMP_LIGHT
881                               "(light-channels=%i),"
882#endif
883                               "(location=%s)",
884           g_sa->rate, g_sa->channels, g_sa->bits,
885#ifndef ROAR_WITHOUT_DCOMP_LIGHT
886           g_light_state.channels,
887#endif
888           location
889          );
890
891  /* Register a service with SLP */
892  err = SLPReg(hslp,
893               addr,
894               SLP_LIFETIME_MAXIMUM,
895               0,
896               attr,
897               SLP_TRUE,
898               register_slp_callback,
899               &callbackerr);
900  regged = 1;
901 } else if ( unreg && regged ) {
902  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
903  regged = 0;
904 } else {
905  SLPClose(hslp);
906  return -1;
907 }
908
909 /* err may contain an error code that occurred as the slp library    */
910 /* _prepared_ to make the call.                                     */
911 if ( err != SLP_OK ) {
912  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
913  return -1;
914 }
915
916 /* callbackerr may contain an error code (that was assigned through */
917 /* the callback cookie) that occurred as slp packets were sent on    */
918 /* the wire */
919 if (callbackerr != SLP_OK) {
920  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
921  return -1;
922 }
923
924 SLPClose(hslp);
925 return 0;
926#else
927 return -1;
928#endif
929}
930
931
932// MAIN:
933
934#ifdef ROAR_HAVE_MAIN_ARGS
935int main (int argc, char * argv[]) {
936#else
937int main (void) {
938#endif
939#ifdef ROAR_HAVE_MAIN_ARGS
940 int i;
941 char * k;
942#endif
943#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
944 char user_sock[80]  = {0};
945#endif
946 struct roar_audio_info sa, max_sa;
947 struct roard_config config;
948#ifdef ROAR_HAVE_FORK
949 int    daemon       = 0;
950#endif
951 int    realtime     = 0;
952 int    sysclocksync = 0;
953 char * driver    = NULL;
954 char * device    = NULL;
955#ifdef ROAR_HAVE_MAIN_ARGS
956 char * opts      = NULL;
957#endif
958// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
959#ifdef ROAR_SUPPORT_LISTEN
960 int    port       = ROAR_DEFAULT_PORT;
961 char * sock_addr  = NULL;
962 int    sock_proto = ROAR_PROTO_ROARAUDIO;
963 int    sock_dir   = -1;
964 struct roar_audio_info sock_info = {0, 0, 0, 0};
965#endif
966 int               drvid;
967#ifndef ROAR_WITHOUT_DCOMP_SOURCES
968 char * s_drv     = "cf";
969 char * s_dev     = NULL;
970 char * s_con     = NULL;
971 char * s_opt     = NULL;
972 int    s_prim    = 0;
973#endif
974 char * o_drv     = getenv("ROAR_DRIVER");
975 char * o_dev     = getenv("ROAR_DEVICE");
976 char * o_opts    = NULL;
977 int    o_prim    = 0;
978 int    o_count   = 0;
979#ifndef ROAR_WITHOUT_DCOMP_LIGHT
980 int    light_channels = LIGHT_CHANNELS_DEFAULT;
981#endif
982#ifdef ROAR_DEFAULT_SOCKGRP
983 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
984#else
985 char * sock_grp  = NULL;
986#endif
987 char * sock_user = NULL;
988#ifdef ROAR_SUPPORT_LISTEN
989 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
990#endif
991#ifdef ROAR_HAVE_LIBSLP
992 int    reg_slp   = 0;
993#endif
994#ifdef ROAR_HAVE_LIBX11
995 int    reg_x11   = 0;
996#endif
997#ifdef ROAR_HAVE_CHROOT
998 char * chrootdir = NULL;
999#endif
1000#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1001 struct group   * grp  = NULL;
1002#endif
1003#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1004 struct passwd  * pwd  = NULL;
1005#endif
1006#ifdef ROAR_HAVE_GETSERVBYNAME
1007 struct servent * serv = NULL;
1008#endif
1009 DRIVER_USERDATA_T drvinst;
1010 struct roar_client * self = NULL;
1011#ifdef ROAR_HAVE_LIBDNET
1012 char decnethost[80];
1013#endif
1014#ifdef SUPPORT_PIDFILE
1015 struct roar_vio_calls pidfile_vio;
1016#endif
1017
1018 ROAR_DBG("main(*): starting roard...");
1019
1020 g_standby       =  0;
1021 g_autostandby   =  0;
1022 alive           =  1;
1023#ifdef ROAR_SUPPORT_LISTEN
1024 g_no_listen     =  0;
1025#else
1026 g_terminate     =  1;
1027#endif
1028
1029 g_verbose       = ROAR_DBG_INFO_NONE;
1030
1031 sa.bits     = ROAR_BITS_DEFAULT;
1032 sa.channels = ROAR_CHANNELS_DEFAULT;
1033 sa.rate     = ROAR_RATE_DEFAULT;
1034 sa.codec    = ROAR_CODEC_DEFAULT;
1035
1036 g_sa        = &sa;
1037 g_max_sa    = &max_sa;
1038
1039 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1040
1041 g_config = &config;
1042
1043 if ( init_config() == -1 ) {
1044  ROAR_ERR("Can not init default config!");
1045  return 1;
1046 }
1047
1048 // load config
1049 roar_libroar_get_config();
1050
1051#ifdef ROAR_SUPPORT_LISTEN
1052 if ( init_listening() == -1 ) {
1053  ROAR_ERR("Can not init listening sockets!");
1054  return 1;
1055 }
1056#endif
1057
1058#ifndef ROAR_WITHOUT_DCOMP_MIDI
1059 if ( midi_init_config() == -1 ) {
1060  ROAR_ERR("Can not init MIDI config!");
1061  return 1;
1062 }
1063#endif
1064
1065#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1066 if ( ssynth_init_config() == -1 ) {
1067  ROAR_ERR("Can not init ssynth config!");
1068  return 1;
1069 }
1070#endif
1071
1072#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1073 if ( rdtcs_init_config() == -1 ) {
1074  ROAR_ERR("Can not init RDTCS config!");
1075  return 1;
1076 }
1077#endif
1078
1079 if ( plugins_preinit() == -1 ) {
1080  ROAR_ERR("Can not pre-init plugins!");
1081  return 1;
1082 }
1083
1084#ifdef ROAR_SUPPORT_LISTEN
1085#ifndef ROAR_TARGET_WIN32
1086 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1087#else
1088 sock_addr = ROAR_DEFAULT_HOST;
1089#endif
1090
1091#ifdef ROAR_HAVE_GETUID
1092 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1093  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1094  sock_addr = user_sock;
1095 }
1096#endif
1097
1098 if ( getenv("ROAR_SERVER") != NULL )
1099  sock_addr = getenv("ROAR_SERVER");
1100#endif
1101
1102 if ( clients_init() == -1 ) {
1103  ROAR_ERR("Can not init clients!");
1104  return 1;
1105 }
1106
1107 if ( streams_init() == -1 ) {
1108  ROAR_ERR("Can not init streams!");
1109  return 1;
1110 }
1111
1112 if ( (g_self_client = clients_new()) == -1 ) {
1113  ROAR_ERR("Can not create self client!");
1114  return 1;
1115 }
1116
1117#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1118 if ( sources_init() == -1 ) {
1119  ROAR_ERR("Can not init sources!");
1120  return 1;
1121 }
1122
1123 if ( (sources_set_client(g_self_client)) == -1 ) {
1124  ROAR_ERR("Can not init set source client!");
1125  return 1;
1126 }
1127#endif
1128
1129#ifdef ROAR_HAVE_MAIN_ARGS
1130 for (i = 1; i < argc; i++) {
1131  k = argv[i];
1132
1133  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1134   usage();
1135   return 0;
1136
1137  } else if ( strcmp(k, "--start") == 0 ) {
1138   // this is a no op
1139  } else if ( strcmp(k, "--restart") == 0 ) {
1140#ifdef ROAR_SUPPORT_LISTEN
1141   if ( restart_server(sock_addr, 1) == -1 ) {
1142    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
1143   }
1144#else
1145   ROAR_ERR("--restart not supported");
1146#endif
1147  } else if ( strcmp(k, "--shutdown") == 0 ) {
1148#ifdef ROAR_SUPPORT_LISTEN
1149   if ( restart_server(sock_addr, 1) == -1 ) {
1150    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
1151    return 1;
1152   }
1153   return 0;
1154#else
1155   ROAR_ERR("--shutdown not supported");
1156   return 1;
1157#endif
1158  } else if ( strcmp(k, "--stop") == 0 ) {
1159#ifdef ROAR_SUPPORT_LISTEN
1160   if ( restart_server(sock_addr, 0) == -1 ) {
1161    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
1162    return 1;
1163   }
1164   return 0;
1165#else
1166   ROAR_ERR("--stop not supported");
1167   return 1;
1168#endif
1169
1170
1171  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1172#ifdef ROAR_HAVE_FORK
1173   daemon = 1;
1174#else
1175   ROAR_ERR("--daemon not supported");
1176#endif
1177  } else if ( strcmp(k, "--verbose") == 0 ) {
1178   g_verbose++;
1179  } else if ( strcmp(k, "--terminate") == 0 ) {
1180   g_terminate = 1;
1181  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1182   sysclocksync = 1000;
1183  } else if ( strcmp(k, "--realtime") == 0 ) {
1184   realtime++;
1185  } else if ( strcmp(k, "--chroot") == 0 ) {
1186#ifdef ROAR_HAVE_CHROOT
1187   chrootdir = argv[++i];
1188#else
1189   ROAR_ERR("--chroot not supported");
1190   i++;
1191#endif
1192  } else if ( strcmp(k, "--setgid") == 0 ) {
1193#ifdef ROAR_HAVE_SETGID
1194   setids |= R_SETGID;
1195#else
1196   ROAR_ERR("--setgid not supported");
1197#endif
1198  } else if ( strcmp(k, "--setuid") == 0 ) {
1199#ifdef ROAR_HAVE_SETUID
1200   setids |= R_SETUID;
1201#else
1202   ROAR_ERR("--setuid not supported");
1203#endif
1204  } else if ( strcmp(k, "--location") == 0 ) {
1205   g_config->location = argv[++i];
1206  } else if ( strcmp(k, "--pidfile") == 0 ) {
1207#ifdef SUPPORT_PIDFILE
1208   pidfile = argv[++i];
1209#else
1210   ROAR_ERR("--pidfile not supported");
1211   i++;
1212#endif
1213  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1214#ifdef ROAR_HAVE_SYSLOG
1215   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1216#else
1217   ROAR_ERR("--log-syslog not supported");
1218#endif
1219
1220
1221  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1222   if ( plugins_load(argv[++i]) == -1 ) {
1223    ROAR_ERR("Can not load plugin");
1224   }
1225
1226  } else if ( strcmp(k, "--list-cf") == 0 ) {
1227   print_codecfilterlist();
1228   return 0;
1229
1230  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1231   sa.rate = atoi(argv[++i]);
1232  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1233   sa.bits = atoi(argv[++i]);
1234  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1235   sa.channels = atoi(argv[++i]);
1236
1237  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1238   if ( update_stream_flags(argv[++i]) == -1 ) {
1239    ROAR_ERR("Can not set stream flags");
1240    return 1;
1241   }
1242
1243  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1244   driver = argv[++i];
1245   if ( strcmp(driver, "list") == 0 ) {
1246    ROAR_WARN("The option is obsolete, use --list-driver!");
1247    print_driverlist();
1248    return 0;
1249   }
1250  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1251   device = argv[++i];
1252  } else if ( strcmp(k, "-dO") == 0 ) {
1253   opts = argv[++i];
1254  } else if ( strcmp(k, "--list-driver") == 0 ) {
1255   print_driverlist();
1256   return 0;
1257
1258  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1259   o_drv  = argv[++i];
1260  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1261   o_dev  = argv[++i];
1262  } else if ( strcmp(k, "-oO") == 0 ) {
1263   o_opts = argv[++i];
1264  } else if ( strcmp(k, "-oP") == 0 ) {
1265   o_prim = 1;
1266  } else if ( strcmp(k, "-oN") == 0 ) {
1267   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1268    o_count++;
1269
1270   o_drv  = o_dev = o_opts = NULL;
1271   o_prim = 0;
1272
1273  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1274#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1275   s_drv = argv[++i];
1276#else
1277   ROAR_ERR("main(*): No support for sources compiled in");
1278#endif
1279  } else if ( strcmp(k, "-S") == 0 ) {
1280#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1281   s_dev = argv[++i];
1282#else
1283   ROAR_ERR("main(*): No support for sources compiled in");
1284#endif
1285  } else if ( strcmp(k, "-sO") == 0 ) {
1286#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1287   s_opt = argv[++i];
1288#else
1289   ROAR_ERR("main(*): No support for sources compiled in");
1290#endif
1291  } else if ( strcmp(k, "-sC") == 0 ) {
1292#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1293   s_con = argv[++i];
1294#else
1295   ROAR_ERR("main(*): No support for sources compiled in");
1296#endif
1297  } else if ( strcmp(k, "-sP") == 0 ) {
1298#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1299   s_prim = 1;
1300#else
1301   ROAR_ERR("main(*): No support for sources compiled in");
1302#endif
1303  } else if ( strcmp(k, "-sN") == 0 ) {
1304#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1305   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1306    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1307   }
1308   s_opt = s_dev = s_con = NULL;
1309   s_drv = "cf";
1310   s_prim = 0;
1311#else
1312   ROAR_ERR("main(*): No support for sources compiled in");
1313#endif
1314  } else if ( strcmp(k, "--list-sources") == 0 ) {
1315#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1316   print_sourcelist();
1317   return 0;
1318#else
1319   ROAR_ERR("main(*): No support for sources compiled in");
1320   return 1;
1321#endif
1322
1323  } else if ( strcmp(k, "--light-channels") == 0 ) {
1324#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1325   light_channels = atoi(argv[++i]);
1326#else
1327   ROAR_WARN("main(*): no light subsystem compiled in");
1328#endif
1329
1330  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1331#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1332   g_rdtcs.rds.pi = atoi(argv[++i]);
1333#else
1334   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1335#endif
1336  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1337#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1338   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1339    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
1340    return 1;
1341   }
1342#else
1343   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1344#endif
1345  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1346#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1347   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1348    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1349    return 1;
1350   }
1351#else
1352   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1353#endif
1354  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1355#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1356   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1357    ROAR_ERR("Can not set RDS TP flag");
1358    return 1;
1359   }
1360#else
1361   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1362#endif
1363  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1364#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1365   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1366    ROAR_ERR("Can not set RDS CT flag");
1367    return 1;
1368   }
1369#else
1370   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1371#endif
1372
1373
1374  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1375#ifndef ROAR_WITHOUT_DCOMP_CB
1376   midi_config.init_cb = 0;
1377#else
1378   // no warning here as this is the disable option
1379#endif
1380  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1381#ifndef ROAR_WITHOUT_DCOMP_CB
1382   midi_config.init_cb = 1;
1383#else
1384   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1385#endif
1386  } else if ( strcmp(k, "--midi-console") == 0 ) {
1387#ifndef ROAR_WITHOUT_DCOMP_CB
1388   midi_config.console_dev = argv[++i];
1389   midi_config.init_cb = 1;
1390#else
1391   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1392#endif
1393
1394  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1395#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1396   ssynth_conf.enable = 1;
1397#else
1398   ROAR_ERR("main(*): No support for ssynth compiled in");
1399#endif
1400  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1401#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1402   ssynth_conf.enable = 0;
1403#else
1404   // we can safely ignore the disable
1405#endif
1406
1407  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
1408#ifdef ROAR_HAVE_LIBX11
1409   x11display = argv[++i];
1410#else
1411   ROAR_ERR("No X11 support compiled in!");
1412   return 1;
1413#endif
1414
1415
1416  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
1417   // This is only usefull in INET not UNIX mode.
1418#ifdef ROAR_SUPPORT_LISTEN
1419   if ( *sock_addr == '/' )
1420    sock_addr = ROAR_DEFAULT_HOST;
1421
1422   errno = 0;
1423   if ( (port = atoi(argv[++i])) < 1 ) {
1424#ifdef ROAR_HAVE_GETSERVBYNAME
1425    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1426     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1427     return 1;
1428    }
1429    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1430    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1431            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1432    port = ROAR_NET2HOST16(serv->s_port);
1433#else
1434    ROAR_ERR("invalite port number: %s", argv[i]);
1435    return 1;
1436#endif
1437   }
1438#endif
1439  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
1440#ifdef ROAR_SUPPORT_LISTEN
1441   sock_addr = argv[++i];
1442#endif
1443
1444  } else if ( strcmp(k, "--proto") == 0 ) {
1445#ifdef ROAR_SUPPORT_LISTEN
1446   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
1447    ROAR_ERR("Unknown protocol: %s", argv[i]);
1448    return 1;
1449   }
1450#endif
1451  } else if ( strcmp(k, "--proto-dir") == 0 ) {
1452#ifdef ROAR_SUPPORT_LISTEN
1453   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
1454    ROAR_ERR("Unknown stream direction: %s", argv[i]);
1455    return 1;
1456   }
1457#endif
1458  } else if ( strcmp(k, "--proto-rate") == 0 ) {
1459#ifdef ROAR_SUPPORT_LISTEN
1460   sock_info.rate = atoi(argv[++i]);
1461#endif
1462  } else if ( strcmp(k, "--proto-bits") == 0 ) {
1463#ifdef ROAR_SUPPORT_LISTEN
1464   sock_info.bits = atoi(argv[++i]);
1465#endif
1466  } else if ( strcmp(k, "--proto-chans") == 0 ) {
1467#ifdef ROAR_SUPPORT_LISTEN
1468   sock_info.channels = atoi(argv[++i]);
1469#endif
1470  } else if ( strcmp(k, "--proto-codec") == 0 ) {
1471#ifdef ROAR_SUPPORT_LISTEN
1472   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
1473    ROAR_ERR("Unknown codec: %s", argv[i]);
1474    return 1;
1475   }
1476#endif
1477
1478
1479  } else if ( strcmp(k, "--list-proto") == 0 ) {
1480   list_proto();
1481   return 0;
1482
1483  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
1484#ifdef ROAR_SUPPORT_LISTEN
1485   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1486    sock_type = ROAR_SOCKET_TYPE_TCP;
1487
1488   if ( *sock_addr == '/' )
1489    sock_addr = ROAR_DEFAULT_HOST;
1490#endif
1491
1492  } else if ( strcmp(k, "-4") == 0 ) {
1493#ifdef ROAR_SUPPORT_LISTEN
1494   sock_type = ROAR_SOCKET_TYPE_TCP;
1495   if ( *sock_addr == '/' )
1496    sock_addr = ROAR_DEFAULT_HOST;
1497#endif
1498  } else if ( strcmp(k, "-6") == 0 ) {
1499#ifdef ROAR_SUPPORT_LISTEN
1500#ifdef AF_INET6
1501   sock_type = ROAR_SOCKET_TYPE_TCP6;
1502   if ( *sock_addr == '/' )
1503    sock_addr = ROAR_DEFAULT_HOST;
1504#else
1505    ROAR_ERR("No IPv6 support compiled in!");
1506    return 1;
1507#endif
1508#endif
1509
1510  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1511#ifdef ROAR_SUPPORT_LISTEN
1512   // ignore this case as it is the default behavor.
1513   sock_type = ROAR_SOCKET_TYPE_UNIX;
1514#endif
1515
1516  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1517#ifdef ROAR_SUPPORT_LISTEN
1518#ifdef ROAR_HAVE_LIBDNET
1519    port   = ROAR_DEFAULT_NUM;
1520    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1521    sock_addr = decnethost;
1522    sock_type = ROAR_SOCKET_TYPE_DECNET;
1523#else
1524    ROAR_ERR("No DECnet support compiled in!");
1525    return 1;
1526#endif
1527#endif
1528  } else if ( strcmp(k, "--new-sock") == 0 ) {
1529#ifdef ROAR_SUPPORT_LISTEN
1530   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1531    ROAR_ERR("Can not open listen socket!");
1532    return 1;
1533   }
1534#endif
1535
1536  } else if ( strcmp(k, "--slp") == 0 ) {
1537#ifdef ROAR_HAVE_LIBSLP
1538   reg_slp = 1;
1539#else
1540   ROAR_ERR("No OpenSLP support compiled in!");
1541   return 1;
1542#endif
1543
1544  } else if ( strcmp(k, "--x11") == 0 ) {
1545#ifdef ROAR_HAVE_LIBX11
1546   reg_x11 = 1;
1547#else
1548   ROAR_ERR("No X11 support compiled in!");
1549   return 1;
1550#endif
1551
1552
1553  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
1554   g_config->jumbo_mtu = atoi(argv[++i]);
1555
1556  } else if ( strcmp(k, "-G") == 0 ) {
1557   sock_grp  = argv[++i];
1558  } else if ( strcmp(k, "-U") == 0 ) {
1559   sock_user = argv[++i];
1560
1561  } else if ( strcmp(k, "--no-listen") == 0 ) {
1562#ifdef ROAR_SUPPORT_LISTEN
1563   sock_addr   = "";
1564   g_terminate = 1;
1565   g_no_listen = 1;
1566#endif
1567  } else if ( strcmp(k, "--client-fh") == 0 ) {
1568   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
1569    ROAR_ERR("main(*): Can not set client's fh");
1570    return 1;
1571   }
1572  } else if ( strcmp(k, "--close-fh") == 0 ) {
1573#ifdef ROAR_HAVE_IO_POSIX
1574   close(atoi(argv[++i]));
1575#else
1576   i++;
1577   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1578#endif
1579
1580  } else if ( strcmp(k, "--standby") == 0 ) {
1581   g_standby = 1;
1582  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1583   g_autostandby = 1;
1584  } else {
1585   usage();
1586   return 1;
1587  }
1588
1589 }
1590#endif
1591
1592#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1593 if ( s_dev != NULL ) {
1594  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1595   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1596  }
1597 }
1598#endif
1599
1600 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
1601
1602 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1603
1604 if ( waveform_init() == -1 ) {
1605  ROAR_ERR("Can not initialize Waveform subsystem");
1606  return 1;
1607 }
1608
1609#ifndef ROAR_WITHOUT_DCOMP_MIDI
1610 if ( midi_init() == -1 ) {
1611  ROAR_ERR("Can not initialize MIDI subsystem");
1612 }
1613#endif
1614
1615#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1616 if ( ssynth_init() == -1 ) {
1617  ROAR_ERR("Can not initialize ssynth subsystem");
1618 }
1619#endif
1620
1621#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1622 if ( light_init(light_channels) == -1 ) {
1623  ROAR_ERR("Can not initialize light control subsystem");
1624 }
1625#endif
1626
1627#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1628 if ( rdtcs_init() == -1 ) {
1629  ROAR_ERR("Can not initialize RDTCS subsystem");
1630 }
1631#endif
1632
1633 if ( plugins_init() == -1 ) {
1634  ROAR_ERR("Can not initialize plugins");
1635 }
1636
1637#ifdef ROAR_SUPPORT_LISTEN
1638 if ( !g_no_listen ) {
1639  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1640   ROAR_ERR("Can not open listen socket!");
1641   return 1;
1642  }
1643 }
1644#endif
1645
1646 if ( output_buffer_init(&sa) == -1 ) {
1647  ROAR_ERR("Can not init output buffer!");
1648  return 1;
1649 }
1650
1651 if ( driver == NULL ) {
1652  driver = "null";
1653 } else {
1654  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1655 }
1656
1657 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1658  ROAR_ERR("Can not open output driver!");
1659  return 1;
1660 }
1661
1662 if ( samples_init() == -1 ) {
1663  ROAR_ERR("Can not init samples!");
1664  return 1;
1665 }
1666
1667
1668 // we should handle this on microcontrollers, too.
1669#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
1670 signal(SIGINT,  on_sig_int);
1671 signal(SIGTERM, on_sig_term);
1672 signal(SIGCHLD, on_sig_chld);
1673 signal(SIGUSR1, on_sig_usr1);
1674 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
1675#endif
1676
1677 if ( realtime ) {
1678#ifdef DEBUG
1679  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1680#endif
1681
1682#ifdef ROAR_HAVE_NICE
1683  errno = 0;
1684  nice(-5*realtime); // -5 for each --realtime
1685  if ( errno ) {
1686   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1687  }
1688#else
1689  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1690#endif
1691/*
1692#ifdef __linux__
1693  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1694   ROAR_WARN("Can not set io priority: %s", strerror(errno));
1695#endif
1696*/
1697 }
1698
1699#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1700 if ( setids & R_SETGID ) {
1701  if ( sock_grp == NULL ) {
1702   ROAR_ERR("Can not set GID if no groupname is supplied");
1703   return 1;
1704  }
1705  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1706   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1707   return 1;
1708  }
1709  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1710   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1711  }
1712  if ( !grp || setgid(grp->gr_gid) == -1 ) {
1713   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1714  }
1715 }
1716#endif
1717
1718
1719 clients_set_pid(g_self_client, getpid());
1720#ifdef ROAR_HAVE_GETUID
1721 clients_set_uid(g_self_client, getuid());
1722#endif
1723#ifdef ROAR_HAVE_GETGID
1724 clients_set_gid(g_self_client, getgid());
1725#endif
1726 clients_get(g_self_client, &self);
1727
1728 if ( self == NULL ) {
1729  ROAR_ERR("Can not get self client!");
1730  return 1;
1731 }
1732
1733 strcpy(self->name, "RoarAudio daemon internal");
1734
1735 if ( roar_nnode_free(&(self->nnode)) == -1 )
1736  return 1;
1737
1738 // not fully correct but ok as workaorund
1739 // so tools assume that roard runs on the same machine as
1740 // they in case they use AF_UNIX:
1741 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
1742  return 1;
1743
1744#ifdef ROAR_HAVE_FORK
1745 if ( daemon ) {
1746#ifdef ROAR_HAVE_SYSLOG
1747  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1748#else
1749  roar_debug_set_stderr_fh(-1);
1750#endif
1751
1752  close(ROAR_STDIN );
1753  close(ROAR_STDOUT);
1754  close(ROAR_STDERR);
1755
1756  if ( fork() )
1757   ROAR_U_EXIT(0);
1758
1759#ifdef ROAR_HAVE_SETSID
1760  setsid();
1761#endif
1762  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1763 }
1764#endif
1765
1766#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1767 // early test for UID as we need this for the pidfile and the setuid()
1768 if ( sock_user != NULL ) {
1769  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1770   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1771   return 1;
1772  }
1773 }
1774#endif
1775
1776#ifdef SUPPORT_PIDFILE
1777 if ( pidfile != NULL ) {
1778  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1779   ROAR_ERR("Can not write pidfile: %s", pidfile);
1780  } else {
1781   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1782   roar_vio_close(&pidfile_vio);
1783  }
1784#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1785  if ( pwd || grp ) {
1786   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
1787    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
1788   }
1789  }
1790  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
1791   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
1792  }
1793#endif
1794 }
1795#endif
1796
1797#ifdef ROAR_HAVE_CHROOT
1798 if (chrootdir) {
1799  if ( chroot(chrootdir) == -1 ) {
1800   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1801   return 2;
1802  }
1803  if ( chdir("/") == -1 ) {
1804   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1805   return 2;
1806  }
1807 }
1808#endif
1809
1810#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1811 if ( setids & R_SETUID ) {
1812  if ( sock_user == NULL ) {
1813   ROAR_ERR("Can not set UID if no username is supplied");
1814   return 1;
1815  }
1816  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1817   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1818   return 3;
1819  }
1820#ifdef ROAR_HAVE_GETUID
1821  clients_set_uid(g_self_client, getuid());
1822#endif
1823 }
1824#endif
1825
1826 // Register with OpenSLP:
1827#ifdef ROAR_HAVE_LIBSLP
1828 if ( reg_slp ) {
1829  register_slp(0, sock_addr);
1830 }
1831#endif
1832
1833#ifdef ROAR_HAVE_LIBX11
1834 if ( reg_x11 ) {
1835  register_x11(0, sock_addr);
1836 }
1837#endif
1838
1839 // start main loop...
1840 main_loop(drvid, drvinst, &sa, sysclocksync);
1841
1842 // clean up.
1843 clean_quit_prep();
1844 driver_close(drvinst, drvid);
1845 output_buffer_free();
1846
1847 return 0;
1848}
1849
1850void cleanup_listen_socket (int terminate) {
1851 int i;
1852
1853 // Deregister from SLP:
1854#ifdef ROAR_HAVE_LIBSLP
1855 register_slp(1, NULL);
1856#endif
1857
1858#ifdef ROAR_HAVE_LIBX11
1859 register_x11(1, NULL);
1860#endif
1861
1862#ifdef ROAR_SUPPORT_LISTEN
1863 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1864  if ( g_listen[i].used  ) {
1865   roar_vio_close(&(g_listen[i].sock));
1866
1867   g_listen[i].used = 0;
1868
1869#ifdef ROAR_HAVE_UNIX
1870   if ( server[i] != NULL )
1871    if ( *(server[i]) == '/' )
1872     unlink(server[i]);
1873#endif
1874  }
1875 }
1876
1877#endif
1878
1879 if ( terminate )
1880  g_terminate = 1;
1881}
1882
1883void clean_quit_prep (void) {
1884 cleanup_listen_socket(0);
1885
1886 plugins_free();
1887
1888#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1889 sources_free();
1890#endif
1891 streams_free();
1892 clients_free();
1893#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1894 ssynth_free();
1895#endif
1896#ifndef ROAR_WITHOUT_DCOMP_CB
1897 midi_cb_stop(); // stop console beep
1898#endif
1899#ifndef ROAR_WITHOUT_DCOMP_MIDI
1900 midi_free();
1901#endif
1902#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1903 light_free();
1904#endif
1905#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1906 rdtcs_free();
1907#endif
1908
1909 waveform_free();
1910
1911#ifdef SUPPORT_PIDFILE
1912 if ( pidfile != NULL )
1913  unlink(pidfile);
1914#endif
1915}
1916
1917void clean_quit (void) {
1918 clean_quit_prep();
1919// driver_close(drvinst, drvid);
1920// output_buffer_free();
1921 exit(0);
1922}
1923
1924//ll
Note: See TracBrowser for help on using the repository browser.