source: roaraudio/roard/roard.c @ 3733:78a938545a68

Last change on this file since 3733:78a938545a68 was 3733:78a938545a68, checked in by phi, 14 years ago

return correct error value

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