source: roaraudio/roard/roard.c @ 3954:b8440be58632

Last change on this file since 3954:b8440be58632 was 3954:b8440be58632, checked in by phi, 14 years ago

added --proto-aiprofile

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