source: roaraudio/roard/roard.c @ 3731:53095de69773

Last change on this file since 3731:53095de69773 was 3731:53095de69773, checked in by phi, 14 years ago

corrected prototype

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