source: roaraudio/roard/roard.c @ 4419:f69bdc2096d0

Last change on this file since 4419:f69bdc2096d0 was 4418:0234c5231754, checked in by phi, 14 years ago

make notify debug code runtime activatable.

File size: 70.7 KB
RevLine 
[0]1//roard.c:
2
[668]3/*
[3358]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
[668]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[668]23 *
24 */
25
[0]26#include "roard.h"
27
[1494]28#ifdef ROAR_SUPPORT_LISTEN
[2530]29char * server[ROAR_MAX_LISTEN_SOCKETS];
[1494]30#endif
[60]31
[2104]32#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
33#define SUPPORT_PIDFILE
34char * pidfile = NULL;
35#endif
36
[2530]37#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
[3374]38int    setids    = 0;
39#endif
40
41#ifdef ROAR_HAVE_LIBX11
42char * x11display = NULL;
[2530]43#endif
44
[4141]45int add_output (char * drv, char * dev, char * opts, int prim, int count);
46
[4324]47void dbg_notify_cb(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
48 char buf[1024] = "";
[4338]49 char estr[1024] = "/* ROAR_??? */";
50 char ttstr[1024] = "/* ROAR_OT_??? */";
51 const char * ttname;
52 uint32_t ev = ROAR_EVENT_GET_TYPE(event);
53 int i;
54
55 ttname = roar_ot2str(event->target_type);
56 if ( ttname != NULL ) {
57  snprintf(ttstr, sizeof(ttstr)-1, "/* ROAR_OT_%s */", ttname);
58  buf[sizeof(ttstr)-1] = 0;
59  for (i = 0; ttstr[i] != 0; i++)
60   if ( islower(ttstr[i]) )
61    ttstr[i] = toupper(ttstr[i]);
62 }
63
64 if ( ev == ROAR_NOTIFY_SPECIAL ) {
65  snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_SPECIAL */");
66 } else if ( ROAR_NOTIFY_IS_CMD(ev) ) {
[4345]67  if ( command_get_name(ROAR_NOTIFY_EVENT2CMD(ev), &ttname) == -1 ) {
68   snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_CMD2EVENT(%i) */", ROAR_NOTIFY_EVENT2CMD(ev));
69  } else {
70   snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_CMD2EVENT(ROAR_CMD_%s) */", ttname);
71   for (i = 0; estr[i] != 0; i++)
72    if ( islower(estr[i]) )
73     estr[i] = toupper(estr[i]);
74  }
[4338]75 } else if ( ROAR_NOTIFY_IS_EGRP(ev) ) {
76  snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_EGRP2EVENT(%i) */", ROAR_NOTIFY_EVENT2EGRP(ev));
77 } else if ( ROAR_NOTIFY_IS_OE(ev) ) {
78  switch (ev) {
[4418]79   // OE basics:
[4338]80   case ROAR_OE_BASICS_CHANGE_STATE:
81     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_CHANGE_STATE */");
82    break;
83   case ROAR_OE_BASICS_CHANGE_FLAGS:
84     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_CHANGE_FLAGS */");
85    break;
[4345]86   case ROAR_OE_BASICS_NEW:
87     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_NEW */");
88    break;
89   case ROAR_OE_BASICS_DELETE:
90     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_DELETE */");
91    break;
[4418]92   // OE Streams:
93   case ROAR_OE_STREAM_CHANGE_VOLUME:
94     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_CHANGE_VOLUME */");
95    break;
96   case ROAR_OE_STREAM_XRUN:
97     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_XRUN */");
98    break;
99   // OE Default:
[4338]100   default:
101     snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_OE2EVENT(%i) */", ROAR_NOTIFY_EVENT2OE(ev));
102    break;
103  }
104 } else if ( ROAR_NOTIFY_IS_USER(ev) ) {
105  snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_USER2EVENT(%i) */", ROAR_NOTIFY_EVENT2USER(ev));
106 }
107
108 buf[sizeof(estr)-1] = 0;
[4324]109
110 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) {
[4338]111  snprintf(buf, sizeof(buf)-1, ".event_proxy=0x%.8x%s, ", (int)event->event_proxy, estr);
[4324]112  buf[sizeof(buf)-1] = 0;
113 }
114
[4418]115 ROAR_INFO("dbg_notify_cb(core=%p, event=%p{.flags=0x%.8x, event=0x%.8x%s, %s.emitter=%i, .target=%i, .target_type=%i%s, .arg0=%i, .arg1=%i, .arg2=%p}, userdata=%p) = (void)",
116           0 /* always print: minlevel = 0 */,
[4324]117           core, event,
118           (int)event->flags,
119           (int)event->event,
[4338]120           (event->flags & ROAR_EVENT_FLAG_PROXYEVENT ? "" : estr),
[4324]121           buf,
122           event->emitter,
123           event->target,
124           event->target_type,
[4338]125           ttstr,
[4324]126           event->arg0,
127           event->arg1,
128           event->arg2,
129           userdata);
130}
131
132void dbg_notify_cb_register (void) {
133 struct roar_event event;
134
135 memset(&event, 0, sizeof(event));
136
137 event.event = ROAR_EGRP_ANY_EVENT;
138
139 event.emitter = -1;
140 event.target = -1;
141 event.target_type = -1;
142
143 roar_notify_core_subscribe(NULL, &event, dbg_notify_cb, NULL);
144}
145
[1503]146#ifdef ROAR_HAVE_MAIN_ARGS
[0]147void usage (void) {
148 printf("Usage: roard [OPTIONS]...\n\n");
149
[68]150 printf("Misc Options:\n\n");
151 printf(
[775]152        " --daemon              - Bring the server into background after init\n"
[2970]153        " --verbose             - Be more verbose, can be used multiple times\n"
[71]154        " --terminate           - Terminate after last client quited\n"
[2109]155        " --start               - No op parameter (starting roard is default operation)\n"
[274]156        " --restart             - Trys to stop an old instance and start a new with new settings\n"
[2109]157        " --stop                - Stops a running roard (provide --pidfile!)\n"
158        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
[276]159        " --realtime            - Trys to get realtime priority,\n"
160        "                         give multible times for being more realtime\n"
[4245]161        " --memlock LEVEL       - Set default memory locking level to LEVEL\n"
[444]162        " --chroot DIR          - chroots to the given dir\n"
163        " --setgid              - GroupID to the audio group as specified via -G\n"
164        " --setuid              - UserID to the audio user as specified via -U\n"
[905]165        " --sysclocksync        - calculate exact sample rate using the system clock\n"
[2017]166        " --location  LOC       - Set lion readable location of server\n"
[2104]167#ifdef SUPPORT_PIDFILE
168        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
169#endif
[3688]170#ifdef ROAR_HAVE_SYSLOG
171        " --log-syslog          - Log Warnings, Errors, ... to syslog\n"
172#endif
[68]173       );
174
[3356]175 printf("\nPlugin Options:\n\n");
176 printf(
177        " --plugin-load FILE    - Load plugin FILE\n"
178       );
179
[68]180 printf("\nAudio Options:\n\n");
[0]181 printf(
182        " -R  --rate   RATE     - Set server rate\n"
183        " -B  --bits   BITS     - Set server bits\n"
184        " -C  --chans  CHANNELS - Set server channels\n"
[3963]185        " --aiprofile  PROFILE  - Use the given audio profile\n"
[0]186       );
187
[2339]188 printf("\nStream Options:\n\n");
189 printf(
190        " --stream-flags D=F    - Set default flags for stream directions\n"
[4012]191        "                         D is the stream direction and F is a comma separated\n"
[2339]192        "                         list of flags in form +flag or -flag to set or unset\n"
193        "                         a flag as default or remove it from the default\n"
194       );
195
[932]196 printf("\nOutput Options:\n\n");
[974]197 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
[932]198 printf(" -O  --odevice DEV     - Set the device\n");
199 printf(" -oO OPTS              - Set output options\n");
200 printf(" -oN                   - Adds another output\n");
[961]201 printf(" -oP                   - Mark output as primary\n");
[4225]202#ifdef ROAR_DRIVER_DEFAULT
203 printf(" --list-driver         - List all drivers (default driver: %s)\n", ROAR_DRIVER_DEFAULT);
204#else
205 printf(" --list-driver         - List all drivers (default driver: (autodetect))\n");
206#endif
[932]207
[2485]208#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]209 printf("\nSource Options:\n\n");
210 printf(" -s  --source DRV      - Use DRV as input driver\n"
211        " -S           DEV      - Use DEV as input device\n"
212        " -sO          OPTS     - Use OPTS as input options\n"
[1512]213        " -sN                   - Adds another source\n"
[0]214        " -sP                   - Make souce as primary\n"
215       );
[2270]216 printf(" --list-sources        - List all sources\n");
[2485]217#endif
[0]218
[4358]219#ifndef ROAR_WITHOUT_DCOMP_MIXER
220 printf("\nHardware Mixer Options:\n\n");
221 printf(" -m  --mixer  DRV      - Use DRV as mixer driver\n"
222        " -M           DEV      - Use DEV as mixer device\n"
223        " -mO          OPTS     - Use OPTS as mixer options\n"
224        " -mN                   - Adds another mixer\n"
225        " -mP                   - Make mixer as primary\n"
226       );
227 printf(" --list-mixers         - List all mixers\n");
228#endif
229
[280]230 printf("\nCodec Filter Options:\n\n");
231 printf(" --list-cf             - List all codec filter\n"
232       );
[0]233
[2500]234#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1923]235 printf("\nMIDI Options:\n\n");
[1924]236 printf(" --midi-no-console     - Disable console based MIDI synth\n"
[2444]237        " --midi-console-enable - Enables the console based MIDI synth\n"
[1924]238        " --midi-console DEV    - Set device for MIDI console\n"
[2489]239#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]240        " --ssynth-enable       - Enable simple software synth\n"
241        " --ssynth-disable      - Disable simple software synth\n"
[2489]242#endif
[1924]243       );
[2500]244#endif
[1923]245
[2494]246#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]247 printf("\nLight Control Options:\n\n");
248 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
249                                  LIGHT_CHANNELS_DEFAULT
250       );
[2494]251#endif
[1923]252
[2725]253#ifndef ROAR_WITHOUT_DCOMP_RDTCS
[3339]254 printf("\nRadio Data and Transmitter Control System Options:\n\n");
[2725]255 printf(" --rds-pi   PI         - Sets the RDS Programme Identification (PI)\n"
256        " --rds-ps   PS         - Sets the RDS Programme Service Name (PS)\n"
257        " --rds-pty  PTY        - Sets the RDS Programme Type (PTY)\n"
258        " --rds-tp              - Sets the RDS Traffic Programme (TP) flag\n"
259        " --rds-ct              - Enables sending of RDS Clock Time (CT)\n"
260       );
261#endif
262
[3374]263#ifdef ROAR_HAVE_LIBX11
264 printf("\nX11 Options:\n\n");
265 printf(
266        " --x11-display DISPLAY - Set display for X11\n"
267        " --display DISPLAY     - Set display for X11\n"
268       );
269#endif
270
[0]271 printf("\nServer Options:\n\n");
272 printf(" -t  --tcp             - Use TCP listen socket\n"
[3992]273#ifdef ROAR_HAVE_UNIX
[0]274        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
[3992]275#endif
[508]276#ifdef ROAR_HAVE_LIBDNET
277        " -n  --decnet          - use DECnet listen socket\n"
278#endif
[518]279        " -4                    - Use IPv4 connections (implies -t)\n"
[3837]280#ifdef AF_INET6
[518]281        " -6                    - Use IPv6 connections (implies -t)\n"
282#endif
283#ifdef IPV6_ADDRFORM
284        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
[4012]285        "                         this is normaly not useful.\n"
[518]286#endif
[0]287        " -p  --port            - TCP Port to bind to\n"
288        " -b  --bind            - IP/Hostname to bind to\n"
[1115]289        "     --sock            - Filename for UNIX Domain Socket\n"
[2530]290        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
[3252]291        "     --proto-dir DIR   - Set direction parameter for protocol\n"
292        "     --proto-rate RATE - Set sample rate parameter for protocol\n"
293        "     --proto-bits BITS - Set bits per sample parameter for protocol\n"
294        "     --proto-codec E   - Set codec parameter for protocol\n"
[4012]295        "     --proto-chans C   - Set number of channels parameter for protocol\n"
[3954]296        "     --proto-aiprofile PROFILE\n"
297        "                       - Sets the audio profile for socket\n"
[3956]298        "     --proto-profile P - Set profile for listen socket\n"
[3127]299        "     --list-proto      - List supported protocols\n"
[3956]300        "     --list-profiles   - List supported profiles for --proto-profile\n"
[2530]301        "     --new-sock        - Parameters for new socket follows\n"
[1993]302#ifdef ROAR_HAVE_LIBSLP
303        "     --slp             - Enable OpenSLP support\n"
304#endif
[3373]305#ifdef ROAR_HAVE_LIBX11
306        "     --x11             - Enable X11 support\n"
307#endif
[3039]308        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
[450]309        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
[2737]310        "                         You need the permissions to change the GID\n"
[444]311        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
[2737]312        "                         You need the permissions to change the UID (normaly only root has)\n"
[548]313        " --no-listen           - Do not listen for new clients\n"
[4012]314        "                         (only useful for relaing, impleys --terminate)\n"
[274]315        " --client-fh           - Comunicate with a client over this handle\n"
[4012]316        "                         (only useful for relaing)\n"
[920]317        " --close-fh            - Closes the given fh\n"
318        " --standby             - Start in standby state\n"
319        " --auto-standby        - Automatical goes into standby if there are no streams\n",
[2755]320#ifdef ROAR_DEFAULT_SOCKGRP
[450]321        ROAR_DEFAULT_SOCKGRP
[2755]322#else
323        "(none)"
324#endif
[0]325       );
326// printf("\n Options:\n\n");
327 printf("\n");
328}
[3127]329
330static void list_proto (void) {
331 printf("  Protocol Flag Subsys - Description\n");
332 printf("------------------------------------------------------\n");
333 printf("  roar          WM LRX - RoarAudio native protocol\n");
334#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
335 printf("  esd           W      - EsounD emulation\n");
336#endif
[3603]337#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
[3257]338 printf("  simple        WM LRX - PulseAudio simple protocol\n");
[3597]339#endif
[3684]340#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
341 printf("  rsound        W      - RSound emulation\n");
342#endif
[3981]343#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
344 printf("  rplay         W      - RPlay emulation\n");
345#endif
[3127]346}
347
[1503]348#endif
[0]349
[4101]350#define _pmsg(format, args...) roar_debug_msg(type, __LINE__, __FILE__, ROAR_DBG_PREFIX, format, ## args)
351#define _pmsgc(group, counter, name) _pmsg("  Counter %-10s: %llu", (name), (long long unsigned int)counters_get(group, counter))
352void counters_print(int type, int force) {
353 if ( type != ROAR_DEBUG_TYPE_INFO || force || (ROAR_DBG_INFOVAR) >= ROAR_DBG_INFO_INFO ) {
354  _pmsg("--- Counter Listing ---");
355  _pmsg(" Current:");
356  _pmsgc(cur, clients, "Clients");
357  _pmsgc(cur, streams, "Streams");
358  _pmsg(" Total:");
359  _pmsgc(sum, clients, "Clients");
360  _pmsgc(sum, streams, "Streams");
361  _pmsg("--- End of Counter Listing ---");
362 }
363}
364#undef _pmsgc
365#undef _pmsg
366
[2109]367int restart_server (char * server, int terminate) {
[579]368 struct roar_connection con;
[2111]369#ifdef ROAR_HAVE_KILL
370 char buf[80];
371 ssize_t l;
372 struct roar_vio_calls fh;
373 pid_t pid;
374 int ok;
375
[4055]376 ROAR_INFO("restart_server(server='%s', terminate=%i): trying to restart server", ROAR_DBG_INFO_INFO, server, terminate);
377
[2111]378 if ( pidfile != NULL ) {
379  if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) {
380   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
381  } else {
382   l = roar_vio_read(&fh, buf, 80);
383   roar_vio_close(&fh);
384   if ( l > 0 ) {
385    buf[l-1] = 0;
386    buf[79]  = 0;
387    pid = atoi(buf);
388    if ( terminate ) {
[2113]389     ok = kill(pid, SIGUSR1);
[2111]390    } else {
391     ok = kill(pid, SIGINT);
392    }
393    if ( ok == 0 ) {
394     return 0;
395    } else {
396     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
397    }
398   } else {
399    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
400   }
401  }
402 }
403#endif
[2109]404
[579]405 if ( roar_connect(&con, server) == -1 ) {
406  return -1;
407 }
408
[2109]409 if ( roar_terminate(&con, terminate) == -1 ) {
[579]410  return -1;
411 }
412
413 return roar_disconnect(&con);
414}
415
[444]416#define R_SETUID 1
417#define R_SETGID 2
418
[1609]419int init_config (void) {
420 int i;
421
422 memset(g_config, 0, sizeof(struct roard_config));
423
424 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
425  g_config->streams[i].mixer_channels = 1;
426  g_config->streams[i].mixer.rpg_mul  = 1;
427  g_config->streams[i].mixer.rpg_div  = 1;
428  g_config->streams[i].mixer.scale    = 65535;
429  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
430 }
431
[2291]432 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
[2413]433 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
[2264]434 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
[1899]435 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
[2160]436 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
[1899]437
[2017]438 g_config->location = "***default***";
439
[4245]440 g_config->memlock_level = -1;
441
[1609]442 return 0;
443}
444
[2530]445#ifdef ROAR_SUPPORT_LISTEN
446int init_listening (void) {
447 int i;
448
[3128]449 memset(g_listen, 0, sizeof(g_listen));
450
[2530]451 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3128]452  g_listen[i].proto  = ROAR_PROTO_ROARAUDIO;
[2530]453  server[i]          = NULL;
454 }
455
456 return 0;
457}
458
[3731]459int get_listen(struct roard_listen ** sock, char *** sockname) {
[3730]460 int i;
461
462 if ( sock == NULL )
463  return -1;
464
465 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]466  if ( ! g_listen[i].used ) {
[3730]467   server[i] = NULL;
[3731]468   *sock = &(g_listen[i]);
[3730]469
[3733]470   if ( sockname != NULL )
[3730]471    *sockname = &(server[i]);
[3733]472
473   return 0;
[3730]474  }
475 }
476
477 return -1;
478}
479
[3956]480#ifdef ROAR_SUPPORT_LISTEN
481static struct _listen_profile {
482 const char * name;
483 int          type;
484 int          port;
485 const char * sockaddr;
486 int          proto;
487 int          dir;
488 const char * aiprofile;
489 const char * desc;
490} _g_listen_profiles[] = {
491 // RoarAudio:
[3992]492#ifdef ROAR_HAVE_UNIX
[4058]493 {"roar-usock",     ROAR_SOCKET_TYPE_UNIX,   0,                 "~/" ROAR_DEFAULT_SOCK_USER,
494                    ROAR_PROTO_ROARAUDIO, -1, NULL,
495                    "RoarAudio default user profile"},
496 {"roar-gsock",     ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_SOCK_GLOBAL,
497                    ROAR_PROTO_ROARAUDIO, -1, NULL,
498                    "RoarAudio default global profile"},
[3992]499#endif
500#ifdef ROAR_HAVE_IPV4
[4058]501 {"roar-tcp",       ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PORT, ROAR_DEFAULT_INET4_HOST,
502                    ROAR_PROTO_ROARAUDIO, -1, NULL,
503                    "RoarAudio local TCP profile"},
504 {"roar-tcp-pub",   ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PORT, ROAR_NET_INET4_ANYHOST,
505                    ROAR_PROTO_ROARAUDIO, -1, NULL,
506                    "RoarAudio network TCP profile"},
[3992]507#endif
508#ifdef ROAR_HAVE_LIBDNET
[4058]509 {"roar-dnet",      ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_LISTEN_OBJECT,
510                    ROAR_PROTO_ROARAUDIO, -1, NULL,
511                    "RoarAudio default DECnet"},
[3992]512#endif
513#ifdef ROAR_HAVE_UNIX
[4058]514 {"roar-abstract",  ROAR_SOCKET_TYPE_UNIX,   0,                 "+abstract",        ROAR_PROTO_ROARAUDIO, -1, NULL,
515                    "RoarAudio abstract namespace profile"},
[3992]516#endif
[3956]517
518 // EsounD:
[3992]519#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
520#ifdef ROAR_HAVE_UNIX
[4058]521 {"esd-unix",       ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_ESD_GSOCK,
522                    ROAR_PROTO_ESOUND,    -1, NULL,
523                    "EsounD default local profile"},
[3992]524#endif
525#ifdef ROAR_HAVE_IPV4
[4058]526 {"esd-tcp",        ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET4_LOCALHOST,
527                    ROAR_PROTO_ESOUND,    -1, NULL,
528                    "EsounD local TCP profile"},
529 {"esd-tcp-pub",    ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET4_ANYHOST,
530                    ROAR_PROTO_ESOUND,    -1, NULL,
531                    "EsounD network TCP profile"},
[3992]532#endif
533#endif
[3956]534
535 // RSound:
[3992]536#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
537#ifdef ROAR_HAVE_UNIX
[4058]538 {"rsound-unix",    ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_RSOUND_GSOCK,
539                    ROAR_PROTO_RSOUND,    -1, NULL,
540                    "RSound default local profile"},
[3992]541#endif
542#ifdef ROAR_HAVE_IPV4
[4058]543 {"rsound-tcp",     ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET4_LOCALHOST,
544                    ROAR_PROTO_RSOUND,    -1, NULL,
545                    "RSound local TCP profile"},
546 {"rsound-tcp-pub", ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET4_ANYHOST,
547                    ROAR_PROTO_RSOUND,    -1, NULL,
548                    "RSound network TCP profile"},
[3992]549#endif
550#ifdef ROAR_HAVE_LIBDNET
[4058]551 {"rsound-dnet",    ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_RSOUND_OBJECT,
552                    ROAR_PROTO_RSOUND,    -1, NULL,
553                    "RSound DECnet profile"},
[3992]554#endif
555#endif
[3957]556
557 // PulseAudio Simple:
[3992]558#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
559#ifdef ROAR_HAVE_IPV4
[4058]560 {"pas-play-tcp",   ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PA_PORT, ROAR_NET_INET4_ANYHOST,
561                    ROAR_PROTO_SIMPLE, ROAR_DIR_PLAY, "default",
562                    "PulseAudio Simple TCP play profile"},
563 {"pas-mon-tcp",    ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PA_PORT, ROAR_NET_INET4_ANYHOST,
564                    ROAR_PROTO_SIMPLE, ROAR_DIR_MONITOR, "default",
565                    "PulseAudio Simple TCP monitor profile"},
[3992]566#endif
567#endif
[3987]568
569 // RPlay:
[3992]570#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
571#ifdef ROAR_HAVE_IPV4
[4058]572 {"rplay-tcp",      ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET4_LOCALHOST,
573                    ROAR_PROTO_RPLAY,     -1, NULL,
574                    "RPlay local TCP profile"},
575 {"rplay-tcp-pub",  ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET4_ANYHOST,
576                    ROAR_PROTO_RPLAY,     -1, NULL,
577                    "RPlay network TCP profile"},
[3992]578#endif
579#endif
[3987]580
581 // End of List:
[3956]582 {NULL, -1, -1, NULL, -1, -1, NULL, NULL}
583};
584
585void listen_listen_profiles (void) {
586 struct _listen_profile * p;
587 char * type;
588 int i;
[3987]589 char port[8];
[3956]590
[3990]591 printf("Name           Type    Address          Port    Protocol  Dir        Audio Profile - Description\n");
[3956]592 printf("------------------------------------------------------------------------------------------------\n");
593      //roar-tcp-pub 0.0.0.0       16002   RoarAudio unknown    (null) - (null)
594
595 for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) {
596  switch (p->type) {
597   case ROAR_SOCKET_TYPE_UNIX:   type = "UNIX";   break;
598   case ROAR_SOCKET_TYPE_TCP:    type = "TCP";    break;
599   case ROAR_SOCKET_TYPE_DECNET: type = "DECnet"; break;
600   default:
601     type = "unknown";
602    break;
603  }
[3987]604
605  if ( p->port ) {
[4049]606   snprintf(port, sizeof(port)-1, "%i", p->port);
607   port[sizeof(port)-1] = 0;
[3987]608  } else {
609   strcpy(port, "(none)");
610  }
611
612  printf("%-14s %-7s %-16s %-7s %-9s %-10s %-13s - %s\n",
[3956]613           p->name,
614           type,
[3987]615           p->sockaddr, port,
[3956]616           roar_proto2str(p->proto),
[3957]617           p->dir == -1 ? "(none)" : roar_dir2str(p->dir), p->aiprofile == NULL ? "(none)" : p->aiprofile,
[3956]618           p->desc == NULL ? "" : p->desc);
619 }
620}
621
622int get_listen_profile (const char * name,
623                        int * port, char ** sockaddr, int * type,
624                        int * proto,
625                        int * dir, struct roar_audio_info * info) {
626 static char buf[1024];
627 struct _listen_profile * p;
628 int i;
629
630 for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) {
631  if ( !strcasecmp(p->name, name) ) {
632   *port     = p->port;
633
[3959]634   if ( p->type == ROAR_SOCKET_TYPE_UNIX && p->sockaddr[0] != '+' ) {
635    roar_env_render_path_r(buf, sizeof(buf), p->sockaddr);
636   } else {
637    strncpy(buf, p->sockaddr, sizeof(buf));
638   }
[3956]639   *sockaddr = buf;
640
641   *proto    = p->proto;
642   *dir      = p->dir;
643
644   if ( p->aiprofile != NULL ) {
645    if ( roar_profile2info(info, p->aiprofile) == -1 ) {
646     ROAR_ERR("Unknown audio profile: %s", p->aiprofile);
647     return -1;
648    }
649   }
650   return 0;
651  }
652 }
653
654 return -1;
655}
656#endif
657
[3252]658int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
[2530]659#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
660 struct group   * grp  = NULL;
661#endif
662#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
663 struct passwd  * pwd  = NULL;
664#endif
665#ifdef ROAR_HAVE_UNIX
666 char * env_roar_proxy_backup;
667#endif
668 int    sockid = -1;
[3907]669#ifdef ROAR_HAVE_UNIX
[2530]670 int    sock;
[3907]671#endif
[2530]672 int    i;
673
[4055]674 ROAR_INFO("add_listen(addr='%s', port=%i, sock_type=%i, user='%s', group='%s', proto=%s(%i), dir=%s(%i), info={.rate=%u, .bits=%u, .channels=%u, .codec=%s(%u)}): trying to add listen socket",
675            ROAR_DBG_INFO_INFO,
676            addr, port, sock_type, user, group, roar_proto2str(proto), proto, roar_dir2str(dir), dir,
677            info->rate, info->bits, info->channels, roar_codec2str(info->codec), info->codec);
678
[2530]679 if ( *addr != 0 ) {
680  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]681   if ( ! g_listen[i].used ) {
[2530]682    sockid = i;
683    break;
684   }
685  }
686
687  if ( sockid == -1 )
688   return -1;
689
[3128]690  g_listen[sockid].proto = proto;
[2530]691
692  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
693
[3802]694  if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
[2530]695#ifdef ROAR_HAVE_UNIX
696   if ( *addr == '/' ) {
697    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
698     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
699     unsetenv("ROAR_PROXY");
700    }
701    if ( (sock = roar_socket_connect(addr, port)) != -1 ) {
702     close(sock);
[3040]703     ROAR_ERR("Can not open listen socket: Socket allready in use");
[2530]704     return 1;
705    } else {
706     unlink(addr);
[3802]707     if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
[3040]708      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
[2530]709      return 1;
710     }
711    }
712    if ( env_roar_proxy_backup != NULL ) {
713     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
714     free(env_roar_proxy_backup);
715    }
716#else
717   if (0) { // noop
718#endif
719   } else {
[3041]720    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
[2530]721    return 1;
722   }
723  }
724
725#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[2753]726  if ( group != NULL ) {
727   if ( (grp = getgrnam(group)) == NULL ) {
728    ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
729   }
[2530]730  }
731#endif
732#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[2752]733  if ( user ) {
[2530]734   if ( (pwd = getpwnam(user)) == NULL ) {
735    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
736   }
737  }
738#endif
739
740#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
741  if ( *addr == '/' ) {
[2753]742   if ( grp || pwd ) {
743     if ( chown(addr, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 )
[2530]744      return 1;
[2753]745   }
[2530]746#ifdef ROAR_HAVE_GETUID
[2753]747   if ( grp ) {
[2530]748    if ( getuid() == 0 )
749     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
750      return 1;
[2753]751   }
[2530]752#endif
753  }
754#endif
755 }
756
[2736]757 // in case we opened the listening socket correctly.
[3256]758 if ( dir == -1 )
759  dir = ROAR_DIR_PLAY;
760
[3252]761 g_listen[sockid].inst.stpl.dir = dir;
762 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
[3256]763
764 switch (dir) {
765  case ROAR_DIR_PLAY:
766  case ROAR_DIR_RECORD:
767  case ROAR_DIR_MONITOR:
768  case ROAR_DIR_FILTER:
769  case ROAR_DIR_BIDIR:
770    if ( !g_listen[sockid].inst.stpl.info.rate )
771     g_listen[sockid].inst.stpl.info.rate = g_sa->rate;
772
773    if ( !g_listen[sockid].inst.stpl.info.bits )
774     g_listen[sockid].inst.stpl.info.bits = g_sa->bits;
775
776    if ( !g_listen[sockid].inst.stpl.info.channels )
777     g_listen[sockid].inst.stpl.info.channels = g_sa->channels;
778
779    if ( !g_listen[sockid].inst.stpl.info.codec )
780     g_listen[sockid].inst.stpl.info.codec = g_sa->codec;
781   break;
782 }
783
[3802]784 g_listen[sockid].used = 1;
785 server[sockid]        = addr;
786
[2530]787 return 0;
788}
789#endif
790
[2339]791int update_stream_flags (char * str) {
792 int    dir;
793 char * flags;
794 char * k;
795 int    op;
796 int    flag;
797
798 if ( (flags = strstr(str, "=")) == NULL )
799  return -1;
800
801 *flags = 0;
802  flags++;
803
804 if ( (dir = roar_str2dir(str)) == -1 )
805  return -1;
806
807 while (flags != NULL) {
808  k = flags;
809  flags = strstr(flags, ",");
810
811  if ( flags != NULL )
812   *(flags++) = 0;
813
814  switch (*k) {
[2340]815   case '+': k++; op = ROAR_SET_FLAG;   break;
816   case '-': k++; op = ROAR_RESET_FLAG; break;
[2339]817   default:
[2340]818     op = ROAR_SET_FLAG;
[2339]819  }
820
821  flag = 0;
822
823  if ( !strcmp(k, "sync") ) {
824   flag = ROAR_FLAG_SYNC;
825  } else if ( !strcmp(k, "meta") ) {
826   flag = ROAR_FLAG_META;
827  } else if ( !strcmp(k, "cleanmeta") ) {
828   flag = ROAR_FLAG_CLEANMETA;
829  } else if ( !strcmp(k, "pause") ) {
830   flag = ROAR_FLAG_PAUSE;
831  } else if ( !strcmp(k, "mute") ) {
832   flag = ROAR_FLAG_MUTE;
833  } else if ( !strcmp(k, "antiecho") ) {
834   flag = ROAR_FLAG_ANTIECHO;
[2413]835  } else if ( !strcmp(k, "passmixer") ) {
836   flag = ROAR_FLAG_PASSMIXER;
[2339]837  } else {
838   return -1;
839  }
840
841  g_config->streams[dir].flags |= flag;
842
[2340]843  if ( op == ROAR_RESET_FLAG )
[2339]844   g_config->streams[dir].flags -= flag;
845 }
846
847 return 0;
848}
849
[4140]850#ifdef ROAR_DRIVER_DEFAULT
851#define add_default_output add_output
852#else
853int add_default_output (char * drv, char * dev, char * opts, int prim, int count) {
[4141]854 char * drvs[] = {
[4189]855  // operating system depended things:
856#ifdef __OpenBSD__
857  /* OpenBSD use sndio natively, this check is discusses with upstream (See ML archive August 2010) */
858  "sndio",
859#endif
[4144]860  // native and pseudo-native interfaces:
861  "oss", "alsa", "sndio", "wmm",
862  // sound libs:
863  "ao", "portaudio",
864  // other sound systems:
865  "esd", "rsound", "pulsesimple", "roar",
866  // specal buildins:
867  "sysclock", "null",
868  // terminator:
[4140]869  NULL
870 };
871 int i;
872 int ret;
[4144]873 int _alive;
[4140]874
875 if ( drv != NULL )
876  return add_output(drv, dev, opts, prim, count);
877
878 if ( dev != NULL ) {
[4141]879  ROAR_WARN("add_default_output(drv=(none), dev='%s', opts='%s', prim=%i, count=%i): It's not recommended to use device name without driver name.", dev, opts, prim, count);
[4140]880 }
881
882 for (i = 0; drvs[i] != NULL; i++) {
[4141]883  ROAR_INFO("add_default_output(*): trying driver %s", ROAR_DBG_INFO_INFO, drvs[i]);
[4144]884  _alive = alive; // save global alive setting
885
[4140]886  ret = add_output(drvs[i], dev, opts, prim, count);
887  if ( ret != -1 )
888   return ret;
[4144]889
890  alive = _alive; // restore global alive setting
[4141]891  ROAR_INFO("add_default_output(*): Driver %s faild to load", ROAR_DBG_INFO_VERBOSE, drvs[i]);
[4140]892 }
893
894 return -1;
895}
896#endif
897
[1145]898int add_output (char * drv, char * dev, char * opts, int prim, int count) {
[933]899 int stream;
900 struct roar_stream * s;
901 struct roar_stream_server * ss;
[938]902 char * k, * v;
[1208]903#ifdef ROAR_DRIVER_CODEC
904 char * to_free = NULL;
905#endif
[1926]906 int sync = 0, f_mmap = 0;
[1522]907 int32_t blocks = -1, blocksize = -1;
[1920]908 int dir = ROAR_DIR_OUTPUT;
[1991]909 int error = 0;
910 // DMX:
911 int32_t channel  = -1;
912 int32_t universe = -1;
913 uint16_t tu16;
[2085]914 float q = -32e6;
[933]915
[4055]916 ROAR_INFO("add_output(drv='%s', dev='%s', opts='%s', prim=%i, count=%i): trying to add output driver", ROAR_DBG_INFO_INFO, drv, dev, opts, prim, count);
[933]917
[1145]918 if ( drv == NULL && count == 0 ) {
[4141]919#ifdef ROAR_DRIVER_DEFAULT
[1145]920  drv  = ROAR_DRIVER_DEFAULT;
921  prim = 1;
922  sync = 1;
[1208]923
924#ifdef ROAR_DRIVER_CODEC
925  if ( opts == NULL ) {
926   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
927  }
928#endif
[4141]929#else
930  ROAR_ERR("add_output(*): Can not find default driver");
931  return -1;
932#endif
[1145]933 }
934
[1227]935 if ( opts == NULL && count == 0 ) {
936  sync = 1;
937  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
938 }
939
[2785]940 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
941
[933]942 if ( (stream = streams_new()) == -1 ) {
[938]943  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]944  if ( prim ) alive = 0;
[933]945  return -1;
946 }
947
[2785]948 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
949
[933]950 streams_get(stream, &ss);
951 s = ROAR_STREAM(ss);
952
[2785]953 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
954
[2511]955 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
[933]956
957 s->pos_rel_id = -1;
958// s->info.codec = codec;
959
[2785]960 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
961
[2788]962 if ( opts == NULL ) {
963  k = NULL;
964 } else {
965  k = strtok(opts, ",");
966 }
[2786]967
968 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
969
[938]970 while (k != NULL) {
971//  ROAR_WARN("add_output(*): opts: %s", k);
972
973  if ( (v = strstr(k, "=")) != NULL ) {
974   *v++ = 0;
975  }
976
977  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
978  if ( strcmp(k, "rate") == 0 ) {
979   s->info.rate = atoi(v);
980  } else if ( strcmp(k, "channels") == 0 ) {
981   s->info.channels = atoi(v);
982  } else if ( strcmp(k, "bits") == 0 ) {
983   s->info.bits = atoi(v);
984  } else if ( strcmp(k, "codec") == 0 ) {
[2511]985   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
[938]986    ROAR_ERR("add_output(*): unknown codec '%s'", v);
[1991]987    error++;
[938]988   }
[2085]989  } else if ( strcmp(k, "q") == 0 ) {
990   q = atof(v);
[1221]991  } else if ( strcmp(k, "blocks") == 0 ) {
992   blocks = atoi(v);
[1522]993  } else if ( strcmp(k, "blocksize") == 0 ) {
994   blocksize = atoi(v);
[1926]995  } else if ( strcmp(k, "mmap") == 0 ) {
996   f_mmap = 1;
[1919]997  } else if ( strcmp(k, "subsystem") == 0 ) {
998   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
[1920]999    dir = ROAR_DIR_OUTPUT;
[2494]1000#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1919]1001   } else if ( !strcasecmp(v, "midi") ) {
1002    dir = ROAR_DIR_MIDI_OUT;
[2494]1003#endif
1004#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1919]1005   } else if ( !strcasecmp(v, "light") ) {
1006    dir = ROAR_DIR_LIGHT_OUT;
[2494]1007#endif
1008#ifndef ROAR_WITHOUT_DCOMP_RAW
[2234]1009   } else if ( !strcasecmp(v, "raw") ) {
1010    dir = ROAR_DIR_RAW_OUT;
[2494]1011#endif
[2681]1012   } else if ( !strcasecmp(v, "complex") ) {
1013    dir = ROAR_DIR_COMPLEX_OUT;
[1919]1014   } else {
[2494]1015    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
[1991]1016    error++;
1017   }
1018  // DMX:
1019  } else if ( strcmp(k, "channel") == 0 ) {
1020   channel  = atoi(v);
1021   if ( channel < 0 || channel > 65535 ) {
1022    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
1023    channel = -1;
1024    error++;
[1919]1025   }
[1991]1026  } else if ( strcmp(k, "universe") == 0 ) {
1027   universe = atoi(v);
1028   if ( universe < 0 || universe > 255 ) {
1029    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
1030    universe = -1;
1031    error++;
1032   }
1033
[2115]1034  } else if ( strcmp(k, "name") == 0 ) {
1035   if ( streams_set_name(stream, v) == -1 ) {
1036    ROAR_ERR("add_output(*): Can not set Stream name");
1037    error++;
1038   }
[1991]1039
[1032]1040  } else if ( strcmp(k, "meta") == 0 ) {
1041   streams_set_flag(stream, ROAR_FLAG_META);
[1117]1042  } else if ( strcmp(k, "sync") == 0 ) {
1043   sync = 1;
[1221]1044  } else if ( strcmp(k, "primary") == 0 ) {
1045   prim = 1;
1046
1047  } else if ( strcmp(k, "cleanmeta") == 0 ) {
1048   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
1049  } else if ( strcmp(k, "autoconf") == 0 ) {
[1531]1050   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]1051  } else if ( strcmp(k, "recsource") == 0 ) {
1052   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
1053  } else if ( strcmp(k, "passmixer") == 0 ) {
1054   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
[938]1055  } else {
1056   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]1057   error++;
1058  }
1059
1060  if ( error ) {
[938]1061   streams_delete(stream);
[982]1062   if ( prim ) alive = 0;
[1208]1063#ifdef ROAR_DRIVER_CODEC
1064   if ( to_free != NULL )
1065    free(to_free);
1066#endif
[938]1067   return -1;
1068  }
1069
1070  k = strtok(NULL, ",");
1071 }
1072
[2785]1073 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1074
[2511]1075 // set audio info...
1076 switch (dir) {
1077  case ROAR_DIR_LIGHT_OUT:
1078    switch (s->info.codec) {
1079     case ROAR_CODEC_DMX512:
1080     case -1:
1081       if ( s->info.rate == -1 )
1082        s->info.rate = ROAR_OUTPUT_CFREQ;
1083
1084       s->info.channels =   0;
1085       s->info.bits     =   8;
1086       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
1087      break;
1088    }
1089   break;
1090  case ROAR_DIR_MIDI_OUT:
1091    switch (s->info.codec) {
1092     case ROAR_CODEC_MIDI:
1093     case -1:
1094       if ( s->info.rate == -1 )
1095        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
1096
1097       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
1098       s->info.bits     = ROAR_MIDI_BITS;
1099       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
1100      break;
1101    }
1102   break;
1103  case ROAR_DIR_RAW_OUT:
1104    if ( s->info.rate == -1 )
1105     s->info.rate = 0;
1106    if ( s->info.bits == -1 )
1107     s->info.bits = 0;
1108    if ( s->info.channels == -1 )
1109     s->info.channels = 0;
1110    if ( s->info.codec == -1 )
1111     s->info.codec = 0;
1112   break;
1113 }
1114
1115 if ( s->info.rate == -1 )
1116  s->info.rate = g_sa->rate;
1117 if ( s->info.bits == -1 )
1118  s->info.bits = g_sa->bits;
1119 if ( s->info.channels == -1 )
1120  s->info.channels = g_sa->channels;
1121 if ( s->info.codec == -1 )
1122  s->info.codec = g_sa->codec;
1123
1124 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);
1125
[1919]1126 if ( streams_set_dir(stream, dir, 1) == -1 ) {
1127  streams_delete(stream);
1128  return -1;
1129 }
1130
[1208]1131#ifdef ROAR_DRIVER_CODEC
1132 if ( to_free != NULL )
1133  free(to_free);
1134#endif
1135
[2511]1136 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
[941]1137  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
1138
[2511]1139 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
[933]1140
[2364]1141 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]1142  ss->driver_id = -1; // don't close a driver not opened...
1143  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]1144  streams_delete(stream);
[938]1145  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]1146  if ( prim ) alive = 0;
[933]1147  return -1;
1148 }
1149
[1528]1150 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
1151 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
1152
[1221]1153 if ( blocks != -1 )
1154  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
1155
[1522]1156 if ( blocksize != -1 )
1157  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
1158
[1991]1159 // TODO: we shoudld *really* check for errors here...
1160 if ( channel != -1 ) {
1161  tu16 = channel;
1162  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
1163 }
1164 if ( universe != -1 ) {
1165  tu16 = universe;
1166  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
1167 }
1168
[1156]1169 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
1170
[938]1171 streams_set_fh(stream, -1); // update some internal structures
1172
[2087]1173 if ( q > -1e6 ) {
1174  ROAR_DBG("add_output(*): setting q=%f", q);
1175  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
1176 }
1177
[2490]1178 client_stream_add(g_self_client, stream);
[933]1179
[1200]1180 if ( prim ) {
[961]1181  streams_mark_primary(stream);
[1200]1182  s->pos_rel_id = stream;
1183 }
[961]1184
[1120]1185 if ( sync ) {
[1117]1186  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]1187 } else {
1188  streams_reset_flag(stream, ROAR_FLAG_SYNC);
1189 }
[1117]1190
[1926]1191 if ( f_mmap )
1192  streams_set_flag(stream, ROAR_FLAG_MMAP);
1193
[2511]1194 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);
[933]1195 return 0;
[932]1196}
1197
[4358]1198#ifndef ROAR_WITHOUT_DCOMP_MIXER
1199int add_hwmixer (char * drv, char * dev, char * opts, int prim, int count) {
[4381]1200 char * basename = NULL;
1201 char * subnames = NULL;
1202 char * k, * v;
[4360]1203 int basestream = streams_new();
1204 int ret;
[4381]1205 int error = 0;
[4360]1206//int hwmixer_open(int basestream, char * drv, char * dev, int fh, char * basename, char * subnames) {
1207
[4369]1208 if ( basestream == -1 )
1209  return -1;
1210
[4382]1211 client_stream_add(g_self_client, basestream);
[4369]1212
[4381]1213 if ( opts == NULL ) {
1214  k = NULL;
1215 } else {
1216  k = strtok(opts, ",");
1217 }
1218
1219 while (k != NULL) {
1220//  ROAR_WARN("add_output(*): opts: %s", k);
1221
1222  if ( (v = strstr(k, "=")) != NULL ) {
1223   *v++ = 0;
1224  }
1225
1226  if ( strcmp(k, "primary") == 0 ) {
1227   prim = 1;
1228
1229  } else if ( strcmp(k, "name") == 0 ) {
1230   basename = v;
1231  } else if ( strcmp(k, "subs") == 0 ) {
1232   subnames = v;
1233
1234  } else if ( strcmp(k, "autoconf") == 0 ) {
1235   streams_set_flag(basestream, ROAR_FLAG_AUTOCONF);
1236  } else if ( strcmp(k, "passmixer") == 0 ) {
1237   streams_set_flag(basestream, ROAR_FLAG_PASSMIXER);
1238  } else {
1239   ROAR_ERR("add_hwmixer(*): unknown option '%s'", k);
1240   error++;
1241  }
1242
1243  if ( error ) {
1244   streams_delete(basestream);
1245   if ( prim ) alive = 0;
1246   return -1;
1247  }
1248
1249  k = strtok(NULL, ",");
1250 }
1251
[4360]1252 if ( prim ) {
1253  streams_mark_primary(basestream);
1254 }
1255
[4381]1256 ret = hwmixer_open(basestream, drv, dev, -1, basename, subnames);
[4360]1257
1258 if ( ret == -1 ) {
1259  streams_delete(basestream);
1260 }
1261
1262 return ret == -1 ? -1 : 0;
[4358]1263}
1264#endif
1265
[3373]1266// X11:
1267#ifdef ROAR_HAVE_LIBX11
1268int register_x11 (int unreg, char * sockname) {
1269 struct roar_x11_connection * x11con = NULL;
1270 int ret = 0;
1271
[4103]1272 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
[4155]1273  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
[3373]1274  return -1;
[4103]1275 }
[3373]1276
1277 if ( unreg ) {
[4103]1278  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
[3373]1279   ret = -1;
[4103]1280   ROAR_ERR("Error while unregistereing from X11", ROAR_DBG_INFO_INFO);
1281  } else {
1282   ROAR_INFO("Successfully unregistered from X11", ROAR_DBG_INFO_INFO);
1283  }
[3373]1284 } else {
[4103]1285  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
[3373]1286   ret = -1;
[4103]1287   ROAR_ERR("Error while registereing to X11", ROAR_DBG_INFO_INFO);
1288  } else {
1289   ROAR_INFO("Successfully registered to X11", ROAR_DBG_INFO_INFO);
1290  }
[3373]1291 }
1292
1293 roar_x11_disconnect(x11con);
1294
1295 return ret;
1296}
1297#endif
[1993]1298
1299// SLP:
1300void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
1301 /* return the error code in the cookie */
1302 *(SLPError*)cookie = errcode;
1303}
1304
1305int register_slp (int unreg, char * sockname) {
1306#ifdef ROAR_HAVE_LIBSLP
1307 static int regged = 0;
1308 static char * sn = NULL;
1309 SLPError err;
1310 SLPError callbackerr;
1311 SLPHandle hslp;
1312 char addr[1024];
1313 char attr[1024] = "";
[2017]1314 char * location;
[1993]1315
1316 if ( sockname != NULL )
1317  sn = sockname;
1318
[3076]1319 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
[1993]1320
1321 err = SLPOpen("en", SLP_FALSE, &hslp);
1322
1323 if (err != SLP_OK) {
1324  ROAR_ERR("Error opening slp handle: Error #%i", err);
1325  return -1;
1326 }
1327
1328 if (!unreg) {
[2017]1329
1330  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
1331   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1332   SLPClose(hslp);
1333   return -1;
1334  }
1335
[1995]1336  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2494]1337#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1338                               "(light-channels=%i),"
1339#endif
1340                               "(location=%s)",
[1995]1341           g_sa->rate, g_sa->channels, g_sa->bits,
[2494]1342#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[2017]1343           g_light_state.channels,
[2494]1344#endif
[2017]1345           location
[1995]1346          );
1347
[1993]1348  /* Register a service with SLP */
1349  err = SLPReg(hslp,
1350               addr,
1351               SLP_LIFETIME_MAXIMUM,
1352               0,
1353               attr,
1354               SLP_TRUE,
1355               register_slp_callback,
1356               &callbackerr);
[1994]1357  regged = 1;
1358 } else if ( unreg && regged ) {
1359  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
1360  regged = 0;
1361 } else {
1362  SLPClose(hslp);
1363  return -1;
1364 }
[1993]1365
[1994]1366 /* err may contain an error code that occurred as the slp library    */
1367 /* _prepared_ to make the call.                                     */
1368 if ( err != SLP_OK ) {
1369  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
1370  return -1;
1371 }
1372
1373 /* callbackerr may contain an error code (that was assigned through */
1374 /* the callback cookie) that occurred as slp packets were sent on    */
1375 /* the wire */
1376 if (callbackerr != SLP_OK) {
1377  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
1378  return -1;
[1993]1379 }
1380
1381 SLPClose(hslp);
1382 return 0;
1383#else
1384 return -1;
1385#endif
1386}
1387
1388
1389// MAIN:
1390
[4053]1391#define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \
1392                        ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \
1393                        return 70; \
1394                       }
[4052]1395
[1503]1396#ifdef ROAR_HAVE_MAIN_ARGS
[0]1397int main (int argc, char * argv[]) {
[1503]1398#else
1399int main (void) {
1400#endif
1401#ifdef ROAR_HAVE_MAIN_ARGS
[0]1402 int i;
1403 char * k;
[1503]1404#endif
[2787]1405#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
[905]1406 char user_sock[80]  = {0};
[1494]1407#endif
[1609]1408 struct roar_audio_info sa, max_sa;
1409 struct roard_config config;
[1486]1410#ifdef ROAR_HAVE_FORK
[905]1411 int    daemon       = 0;
[1486]1412#endif
[905]1413 int    realtime     = 0;
1414 int    sysclocksync = 0;
[1207]1415 char * driver    = NULL;
1416 char * device    = NULL;
[1503]1417#ifdef ROAR_HAVE_MAIN_ARGS
[1207]1418 char * opts      = NULL;
[1503]1419#endif
[60]1420// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]1421#ifdef ROAR_SUPPORT_LISTEN
[2530]1422 int    port       = ROAR_DEFAULT_PORT;
1423 char * sock_addr  = NULL;
1424 int    sock_proto = ROAR_PROTO_ROARAUDIO;
[3252]1425 int    sock_dir   = -1;
1426 struct roar_audio_info sock_info = {0, 0, 0, 0};
[1494]1427#endif
[0]1428 int               drvid;
[2486]1429#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1430 char * s_drv     = "cf";
[1110]1431 char * s_dev     = NULL;
[444]1432 char * s_con     = NULL;
1433 char * s_opt     = NULL;
1434 int    s_prim    = 0;
[2486]1435#endif
[1207]1436 char * o_drv     = getenv("ROAR_DRIVER");
1437 char * o_dev     = getenv("ROAR_DEVICE");
[932]1438 char * o_opts    = NULL;
[961]1439 int    o_prim    = 0;
[1145]1440 int    o_count   = 0;
[4358]1441#ifndef ROAR_WITHOUT_DCOMP_MIXER
1442 char * m_drv     = NULL;
1443 char * m_dev     = NULL;
1444 char * m_opts    = NULL;
1445 int    m_prim    = 0;
1446 int    m_count   = 0;
1447#endif
[2494]1448#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1449 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[2494]1450#endif
[2751]1451#ifdef ROAR_DEFAULT_SOCKGRP
[450]1452 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[2751]1453#else
1454 char * sock_grp  = NULL;
1455#endif
[444]1456 char * sock_user = NULL;
[1494]1457#ifdef ROAR_SUPPORT_LISTEN
[517]1458 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]1459#endif
[1993]1460#ifdef ROAR_HAVE_LIBSLP
1461 int    reg_slp   = 0;
1462#endif
[3373]1463#ifdef ROAR_HAVE_LIBX11
1464 int    reg_x11   = 0;
1465#endif
[1486]1466#ifdef ROAR_HAVE_CHROOT
[444]1467 char * chrootdir = NULL;
[1486]1468#endif
1469#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]1470 struct group   * grp  = NULL;
[1486]1471#endif
1472#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]1473 struct passwd  * pwd  = NULL;
[1486]1474#endif
1475#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1476 struct servent * serv = NULL;
[1486]1477#endif
[0]1478 DRIVER_USERDATA_T drvinst;
[39]1479 struct roar_client * self = NULL;
[508]1480#ifdef ROAR_HAVE_LIBDNET
1481 char decnethost[80];
1482#endif
[2104]1483#ifdef SUPPORT_PIDFILE
1484 struct roar_vio_calls pidfile_vio;
1485#endif
[0]1486
[2784]1487 ROAR_DBG("main(*): starting roard...");
1488
[0]1489 g_standby       =  0;
[920]1490 g_autostandby   =  0;
[982]1491 alive           =  1;
[1494]1492#ifdef ROAR_SUPPORT_LISTEN
[1155]1493 g_no_listen     =  0;
[1494]1494#else
1495 g_terminate     =  1;
1496#endif
[0]1497
[2970]1498 g_verbose       = ROAR_DBG_INFO_NONE;
1499
[0]1500 sa.bits     = ROAR_BITS_DEFAULT;
1501 sa.channels = ROAR_CHANNELS_DEFAULT;
1502 sa.rate     = ROAR_RATE_DEFAULT;
1503 sa.codec    = ROAR_CODEC_DEFAULT;
1504
[1609]1505 g_sa        = &sa;
1506 g_max_sa    = &max_sa;
1507
1508 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1509
[4101]1510 counters_init();
1511
[1609]1512 g_config = &config;
1513
1514 if ( init_config() == -1 ) {
1515  ROAR_ERR("Can not init default config!");
1516  return 1;
1517 }
[0]1518
[2968]1519 // load config
1520 roar_libroar_get_config();
1521
[4323]1522 // init notify core:
1523 // TODO: reconsider number of lists.
1524 if ( roar_notify_core_new_global(-1) == -1 ) {
1525  ROAR_ERR("Can not init notify core!");
1526  return 1;
1527 }
1528
[4324]1529 if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) {
1530  ROAR_ERR("Can not init notify core!");
1531  return 1;
1532 }
1533
1534#ifdef DEBUG
[4418]1535 // enable early in case we have DEBUG set.
[4324]1536 dbg_notify_cb_register();
1537#endif
1538
[2530]1539#ifdef ROAR_SUPPORT_LISTEN
1540 if ( init_listening() == -1 ) {
1541  ROAR_ERR("Can not init listening sockets!");
1542  return 1;
1543 }
1544#endif
1545
[2500]1546#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1924]1547 if ( midi_init_config() == -1 ) {
1548  ROAR_ERR("Can not init MIDI config!");
1549  return 1;
1550 }
[2500]1551#endif
[60]1552
[2489]1553#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1554 if ( ssynth_init_config() == -1 ) {
1555  ROAR_ERR("Can not init ssynth config!");
1556  return 1;
1557 }
[2489]1558#endif
[2451]1559
[2718]1560#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1561 if ( rdtcs_init_config() == -1 ) {
1562  ROAR_ERR("Can not init RDTCS config!");
1563  return 1;
1564 }
1565#endif
1566
[3354]1567 if ( plugins_preinit() == -1 ) {
1568  ROAR_ERR("Can not pre-init plugins!");
1569  return 1;
1570 }
1571
[1494]1572#ifdef ROAR_SUPPORT_LISTEN
[2767]1573#ifndef ROAR_TARGET_WIN32
[2530]1574 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
[2767]1575#else
1576 sock_addr = ROAR_DEFAULT_HOST;
1577#endif
1578
[1753]1579#ifdef ROAR_HAVE_GETUID
[1486]1580 if ( getuid() != 0 && getenv("HOME") != NULL ) {
[3959]1581/*
[1486]1582  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[3959]1583*/
1584  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
[2530]1585  sock_addr = user_sock;
[3959]1586  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
[60]1587 }
[1753]1588#endif
[60]1589
[279]1590 if ( getenv("ROAR_SERVER") != NULL )
[2530]1591  sock_addr = getenv("ROAR_SERVER");
[1494]1592#endif
[279]1593
[63]1594 if ( clients_init() == -1 ) {
1595  ROAR_ERR("Can not init clients!");
1596  return 1;
1597 }
1598
1599 if ( streams_init() == -1 ) {
1600  ROAR_ERR("Can not init streams!");
1601  return 1;
1602 }
1603
[64]1604 if ( (g_self_client = clients_new()) == -1 ) {
1605  ROAR_ERR("Can not create self client!");
1606  return 1;
1607 }
1608
[2485]1609#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1610 if ( sources_init() == -1 ) {
1611  ROAR_ERR("Can not init sources!");
1612  return 1;
1613 }
1614
[64]1615 if ( (sources_set_client(g_self_client)) == -1 ) {
1616  ROAR_ERR("Can not init set source client!");
1617  return 1;
1618 }
[2485]1619#endif
[64]1620
[1503]1621#ifdef ROAR_HAVE_MAIN_ARGS
[0]1622 for (i = 1; i < argc; i++) {
1623  k = argv[i];
1624
1625  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1626   usage();
1627   return 0;
1628
[2109]1629  } else if ( strcmp(k, "--start") == 0 ) {
1630   // this is a no op
[579]1631  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]1632#ifdef ROAR_SUPPORT_LISTEN
[2530]1633   if ( restart_server(sock_addr, 1) == -1 ) {
1634    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
[579]1635   }
[1494]1636#else
1637   ROAR_ERR("--restart not supported");
1638#endif
[2109]1639  } else if ( strcmp(k, "--shutdown") == 0 ) {
1640#ifdef ROAR_SUPPORT_LISTEN
[2530]1641   if ( restart_server(sock_addr, 1) == -1 ) {
1642    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
[2109]1643    return 1;
1644   }
1645   return 0;
1646#else
1647   ROAR_ERR("--shutdown not supported");
1648   return 1;
1649#endif
1650  } else if ( strcmp(k, "--stop") == 0 ) {
1651#ifdef ROAR_SUPPORT_LISTEN
[2530]1652   if ( restart_server(sock_addr, 0) == -1 ) {
1653    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
[2109]1654    return 1;
1655   }
1656   return 0;
1657#else
1658   ROAR_ERR("--stop not supported");
1659   return 1;
1660#endif
1661
[579]1662
[775]1663  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]1664#ifdef ROAR_HAVE_FORK
[775]1665   daemon = 1;
[1486]1666#else
1667   ROAR_ERR("--daemon not supported");
1668#endif
[2970]1669  } else if ( strcmp(k, "--verbose") == 0 ) {
1670   g_verbose++;
[71]1671  } else if ( strcmp(k, "--terminate") == 0 ) {
1672   g_terminate = 1;
[905]1673  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1674   sysclocksync = 1000;
[275]1675  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]1676   realtime++;
[4245]1677  } else if ( strcmp(k, "--memlock") == 0 ) {
1678   _CKHAVEARGS(1);
1679   g_config->memlock_level = memlock_str2level(argv[++i]);
[444]1680  } else if ( strcmp(k, "--chroot") == 0 ) {
[4052]1681   _CKHAVEARGS(1);
[1486]1682#ifdef ROAR_HAVE_CHROOT
[444]1683   chrootdir = argv[++i];
[1486]1684#else
1685   ROAR_ERR("--chroot not supported");
1686   i++;
1687#endif
[444]1688  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]1689#ifdef ROAR_HAVE_SETGID
[444]1690   setids |= R_SETGID;
[1486]1691#else
1692   ROAR_ERR("--setgid not supported");
1693#endif
[444]1694  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]1695#ifdef ROAR_HAVE_SETUID
[444]1696   setids |= R_SETUID;
[1486]1697#else
1698   ROAR_ERR("--setuid not supported");
1699#endif
[2017]1700  } else if ( strcmp(k, "--location") == 0 ) {
[4052]1701   _CKHAVEARGS(1);
[2017]1702   g_config->location = argv[++i];
[2104]1703  } else if ( strcmp(k, "--pidfile") == 0 ) {
[4052]1704   _CKHAVEARGS(1);
[2104]1705#ifdef SUPPORT_PIDFILE
1706   pidfile = argv[++i];
1707#else
1708   ROAR_ERR("--pidfile not supported");
1709   i++;
1710#endif
[3688]1711  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1712#ifdef ROAR_HAVE_SYSLOG
1713   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1714#else
1715   ROAR_ERR("--log-syslog not supported");
1716#endif
1717
[68]1718
[3356]1719  } else if ( strcmp(k, "--plugin-load") == 0 ) {
[4052]1720   _CKHAVEARGS(1);
[3356]1721   if ( plugins_load(argv[++i]) == -1 ) {
1722    ROAR_ERR("Can not load plugin");
1723   }
1724
[280]1725  } else if ( strcmp(k, "--list-cf") == 0 ) {
1726   print_codecfilterlist();
1727   return 0;
1728
[0]1729  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
[4052]1730   _CKHAVEARGS(1);
[0]1731   sa.rate = atoi(argv[++i]);
1732  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
[4052]1733   _CKHAVEARGS(1);
[0]1734   sa.bits = atoi(argv[++i]);
1735  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
[4052]1736   _CKHAVEARGS(1);
[0]1737   sa.channels = atoi(argv[++i]);
1738
[3962]1739  } else if ( strcmp(k, "--aiprofile") == 0 ) {
[4052]1740   _CKHAVEARGS(1);
[3962]1741   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
1742    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1743    return 1;
1744   }
1745   sa.codec    = ROAR_CODEC_DEFAULT;
1746
[2339]1747  } else if ( strcmp(k, "--stream-flags") == 0 ) {
[4052]1748   _CKHAVEARGS(1);
[2339]1749   if ( update_stream_flags(argv[++i]) == -1 ) {
1750    ROAR_ERR("Can not set stream flags");
1751    return 1;
1752   }
1753
[0]1754  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
[4052]1755   _CKHAVEARGS(1);
[0]1756   driver = argv[++i];
1757   if ( strcmp(driver, "list") == 0 ) {
[974]1758    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]1759    print_driverlist();
[973]1760    return 0;
[0]1761   }
1762  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
[4052]1763   _CKHAVEARGS(1);
[0]1764   device = argv[++i];
1765  } else if ( strcmp(k, "-dO") == 0 ) {
[4052]1766   _CKHAVEARGS(1);
[0]1767   opts = argv[++i];
[973]1768  } else if ( strcmp(k, "--list-driver") == 0 ) {
1769   print_driverlist();
1770   return 0;
[0]1771
[932]1772  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
[4052]1773   _CKHAVEARGS(1);
[932]1774   o_drv  = argv[++i];
1775  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
[4052]1776   _CKHAVEARGS(1);
[932]1777   o_dev  = argv[++i];
1778  } else if ( strcmp(k, "-oO") == 0 ) {
[4052]1779   _CKHAVEARGS(1);
[932]1780   o_opts = argv[++i];
[961]1781  } else if ( strcmp(k, "-oP") == 0 ) {
1782   o_prim = 1;
[932]1783  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]1784   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1785    o_count++;
1786
[961]1787   o_drv  = o_dev = o_opts = NULL;
1788   o_prim = 0;
[932]1789
[0]1790  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[4052]1791   _CKHAVEARGS(1);
[2485]1792#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1793   s_drv = argv[++i];
[2485]1794#else
1795   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1796   i++;
[2485]1797#endif
[550]1798  } else if ( strcmp(k, "-S") == 0 ) {
[4052]1799   _CKHAVEARGS(1);
[2485]1800#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1801   s_dev = argv[++i];
[2485]1802#else
1803   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1804   i++;
[2485]1805#endif
[0]1806  } else if ( strcmp(k, "-sO") == 0 ) {
[4052]1807   _CKHAVEARGS(1);
[2485]1808#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1809   s_opt = argv[++i];
[2485]1810#else
1811   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1812   i++;
[2485]1813#endif
[0]1814  } else if ( strcmp(k, "-sC") == 0 ) {
[4052]1815   _CKHAVEARGS(1);
[2485]1816#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1817   s_con = argv[++i];
[2485]1818#else
1819   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1820   i++;
[2485]1821#endif
[0]1822  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]1823#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1824   s_prim = 1;
[2485]1825#else
1826   ROAR_ERR("main(*): No support for sources compiled in");
1827#endif
[1110]1828  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]1829#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1830   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1831    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1832   }
1833   s_opt = s_dev = s_con = NULL;
1834   s_drv = "cf";
1835   s_prim = 0;
[2485]1836#else
1837   ROAR_ERR("main(*): No support for sources compiled in");
1838#endif
[2270]1839  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]1840#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]1841   print_sourcelist();
1842   return 0;
[2485]1843#else
1844   ROAR_ERR("main(*): No support for sources compiled in");
[2504]1845   return 1;
[2485]1846#endif
[0]1847
[4358]1848  } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) {
1849   _CKHAVEARGS(1);
1850#ifndef ROAR_WITHOUT_DCOMP_MIXER
1851   m_drv  = argv[++i];
1852#else
1853   ROAR_ERR("main(*): No support for mixer compiled in");
1854   return 1;
1855#endif
1856  } else if ( strcmp(k, "-M") == 0 ) {
1857   _CKHAVEARGS(1);
1858#ifndef ROAR_WITHOUT_DCOMP_MIXER
1859   m_dev  = argv[++i];
1860#else
1861   ROAR_ERR("main(*): No support for mixer compiled in");
1862   return 1;
1863#endif
1864  } else if ( strcmp(k, "-mO") == 0 ) {
1865   _CKHAVEARGS(1);
1866#ifndef ROAR_WITHOUT_DCOMP_MIXER
1867   m_opts = argv[++i];
1868#else
1869   ROAR_ERR("main(*): No support for mixer compiled in");
1870   return 1;
1871#endif
1872  } else if ( strcmp(k, "-mP") == 0 ) {
1873#ifndef ROAR_WITHOUT_DCOMP_MIXER
1874   m_prim = 1;
1875#else
1876   ROAR_ERR("main(*): No support for mixer compiled in");
1877   return 1;
1878#endif
1879  } else if ( strcmp(k, "-mN") == 0 ) {
1880#ifndef ROAR_WITHOUT_DCOMP_MIXER
1881   if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) != -1 )
1882    m_count++;
1883
1884   m_drv  = o_dev = o_opts = NULL;
1885   m_prim = 0;
1886#else
1887   ROAR_ERR("main(*): No support for mixer compiled in");
1888   return 1;
1889#endif
[4360]1890  } else if ( strcmp(k, "--list-mixers") == 0 ) {
1891#ifndef ROAR_WITHOUT_DCOMP_MIXER
1892   print_hwmixerlist();
1893   return 0;
1894#else
1895   ROAR_ERR("main(*): No support for mixer compiled in");
1896   return 1;
1897#endif
[4358]1898
[1923]1899  } else if ( strcmp(k, "--light-channels") == 0 ) {
[4052]1900   _CKHAVEARGS(1);
[2494]1901#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1902   light_channels = atoi(argv[++i]);
[2494]1903#else
1904   ROAR_WARN("main(*): no light subsystem compiled in");
[4052]1905   i++;
[2494]1906#endif
[1923]1907
[2725]1908  } else if ( strcmp(k, "--rds-pi") == 0 ) {
[4052]1909   _CKHAVEARGS(1);
[2725]1910#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1911   g_rdtcs.rds.pi = atoi(argv[++i]);
1912#else
1913   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]1914   i++;
[2725]1915#endif
[2728]1916  } else if ( strcmp(k, "--rds-ps") == 0 ) {
[4052]1917   _CKHAVEARGS(1);
[2728]1918#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1919   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
[2729]1920    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
[2728]1921    return 1;
1922   }
1923#else
1924   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]1925   i++;
[2728]1926#endif
1927  } else if ( strcmp(k, "--rds-pty") == 0 ) {
[4052]1928   _CKHAVEARGS(1);
[2728]1929#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1930   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
[2729]1931    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
[2728]1932    return 1;
1933   }
1934#else
1935   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]1936   i++;
[2728]1937#endif
1938  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1939#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1940   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1941    ROAR_ERR("Can not set RDS TP flag");
1942    return 1;
1943   }
1944#else
1945   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1946#endif
1947  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1948#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1949   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1950    ROAR_ERR("Can not set RDS CT flag");
1951    return 1;
1952   }
1953#else
1954   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1955#endif
1956
[2725]1957
[1924]1958  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]1959#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1960   midi_config.init_cb = 0;
[2487]1961#else
1962   // no warning here as this is the disable option
1963#endif
[2444]1964  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]1965#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]1966   midi_config.init_cb = 1;
[2487]1967#else
1968   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1969#endif
[1924]1970  } else if ( strcmp(k, "--midi-console") == 0 ) {
[4052]1971   _CKHAVEARGS(1);
[2487]1972#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1973   midi_config.console_dev = argv[++i];
[2444]1974   midi_config.init_cb = 1;
[2487]1975#else
1976   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
[4052]1977   i++;
[2487]1978#endif
[1924]1979
[2451]1980  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]1981#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1982   ssynth_conf.enable = 1;
[2489]1983#else
1984   ROAR_ERR("main(*): No support for ssynth compiled in");
1985#endif
[2451]1986  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]1987#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1988   ssynth_conf.enable = 0;
[2489]1989#else
1990   // we can safely ignore the disable
1991#endif
[2451]1992
[3374]1993  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
[4052]1994   _CKHAVEARGS(1);
[3374]1995#ifdef ROAR_HAVE_LIBX11
1996   x11display = argv[++i];
1997#else
1998   ROAR_ERR("No X11 support compiled in!");
1999   return 1;
2000#endif
2001
2002
[0]2003  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[4052]2004   _CKHAVEARGS(1);
[4012]2005   // This is only useful in INET not UNIX mode.
[1494]2006#ifdef ROAR_SUPPORT_LISTEN
[2530]2007   if ( *sock_addr == '/' )
2008    sock_addr = ROAR_DEFAULT_HOST;
[447]2009
[446]2010   errno = 0;
2011   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]2012#ifdef ROAR_HAVE_GETSERVBYNAME
[446]2013    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
2014     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
2015     return 1;
2016    }
2017    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
2018    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
2019            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
2020    port = ROAR_NET2HOST16(serv->s_port);
[1486]2021#else
2022    ROAR_ERR("invalite port number: %s", argv[i]);
2023    return 1;
2024#endif
[446]2025   }
[1494]2026#endif
[1115]2027  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[4052]2028   _CKHAVEARGS(1);
[1494]2029#ifdef ROAR_SUPPORT_LISTEN
[2530]2030   sock_addr = argv[++i];
[4052]2031#else
2032   i++;
[2530]2033#endif
2034
2035  } else if ( strcmp(k, "--proto") == 0 ) {
2036#ifdef ROAR_SUPPORT_LISTEN
[2549]2037   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
[2530]2038    ROAR_ERR("Unknown protocol: %s", argv[i]);
2039    return 1;
2040   }
[1494]2041#endif
[3252]2042  } else if ( strcmp(k, "--proto-dir") == 0 ) {
[4052]2043   _CKHAVEARGS(1);
[3252]2044#ifdef ROAR_SUPPORT_LISTEN
2045   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
2046    ROAR_ERR("Unknown stream direction: %s", argv[i]);
2047    return 1;
2048   }
[4052]2049#else
2050   i++;
[3252]2051#endif
2052  } else if ( strcmp(k, "--proto-rate") == 0 ) {
[4052]2053   _CKHAVEARGS(1);
[3252]2054#ifdef ROAR_SUPPORT_LISTEN
2055   sock_info.rate = atoi(argv[++i]);
[4052]2056#else
2057   i++;
[3252]2058#endif
2059  } else if ( strcmp(k, "--proto-bits") == 0 ) {
[4052]2060   _CKHAVEARGS(1);
[3252]2061#ifdef ROAR_SUPPORT_LISTEN
2062   sock_info.bits = atoi(argv[++i]);
[4052]2063#else
2064   i++;
[3252]2065#endif
2066  } else if ( strcmp(k, "--proto-chans") == 0 ) {
[4052]2067   _CKHAVEARGS(1);
[3252]2068#ifdef ROAR_SUPPORT_LISTEN
2069   sock_info.channels = atoi(argv[++i]);
[4052]2070#else
2071   i++;
[3252]2072#endif
2073  } else if ( strcmp(k, "--proto-codec") == 0 ) {
[4052]2074   _CKHAVEARGS(1);
[3252]2075#ifdef ROAR_SUPPORT_LISTEN
2076   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
2077    ROAR_ERR("Unknown codec: %s", argv[i]);
2078    return 1;
2079   }
[4052]2080#else
2081   i++;
[3252]2082#endif
[3954]2083  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
[4052]2084   _CKHAVEARGS(1);
[3954]2085#ifdef ROAR_SUPPORT_LISTEN
2086   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
2087    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2088    return 1;
2089   }
[4052]2090#else
2091   i++;
[3954]2092#endif
[3956]2093  } else if ( strcmp(k, "--list-profiles") == 0 ) {
2094#ifdef ROAR_SUPPORT_LISTEN
2095   listen_listen_profiles();
2096   return 0;
2097#endif
2098  } else if ( strcmp(k, "--proto-profile") == 0 ) {
[4052]2099   _CKHAVEARGS(1);
[3956]2100#ifdef ROAR_SUPPORT_LISTEN
2101   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
2102    ROAR_ERR("Unknown listen profile: %s", argv[i]);
2103    return 1;
2104   }
[4052]2105#else
2106   i++;
[3956]2107#endif
[3252]2108
2109
[3127]2110  } else if ( strcmp(k, "--list-proto") == 0 ) {
2111   list_proto();
2112   return 0;
[518]2113
[573]2114  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]2115#ifdef ROAR_SUPPORT_LISTEN
[518]2116   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
2117    sock_type = ROAR_SOCKET_TYPE_TCP;
2118
[2530]2119   if ( *sock_addr == '/' )
2120    sock_addr = ROAR_DEFAULT_HOST;
[1494]2121#endif
[518]2122
2123  } else if ( strcmp(k, "-4") == 0 ) {
[1494]2124#ifdef ROAR_SUPPORT_LISTEN
[517]2125   sock_type = ROAR_SOCKET_TYPE_TCP;
[2530]2126   if ( *sock_addr == '/' )
2127    sock_addr = ROAR_DEFAULT_HOST;
[1494]2128#endif
[518]2129  } else if ( strcmp(k, "-6") == 0 ) {
[1494]2130#ifdef ROAR_SUPPORT_LISTEN
[3837]2131#ifdef AF_INET6
[518]2132   sock_type = ROAR_SOCKET_TYPE_TCP6;
[2530]2133   if ( *sock_addr == '/' )
2134    sock_addr = ROAR_DEFAULT_HOST;
[519]2135#else
2136    ROAR_ERR("No IPv6 support compiled in!");
2137    return 1;
2138#endif
[1494]2139#endif
[518]2140
[573]2141  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]2142#ifdef ROAR_SUPPORT_LISTEN
[62]2143   // ignore this case as it is the default behavor.
[517]2144   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]2145#endif
[518]2146
[573]2147  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]2148#ifdef ROAR_SUPPORT_LISTEN
[508]2149#ifdef ROAR_HAVE_LIBDNET
2150    port   = ROAR_DEFAULT_NUM;
2151    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
[2530]2152    sock_addr = decnethost;
[517]2153    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]2154#else
2155    ROAR_ERR("No DECnet support compiled in!");
2156    return 1;
2157#endif
[1494]2158#endif
[2530]2159  } else if ( strcmp(k, "--new-sock") == 0 ) {
2160#ifdef ROAR_SUPPORT_LISTEN
[3252]2161   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
[2530]2162    ROAR_ERR("Can not open listen socket!");
2163    return 1;
2164   }
2165#endif
[518]2166
[1993]2167  } else if ( strcmp(k, "--slp") == 0 ) {
2168#ifdef ROAR_HAVE_LIBSLP
2169   reg_slp = 1;
2170#else
[3373]2171   ROAR_ERR("No OpenSLP support compiled in!");
2172   return 1;
[1993]2173#endif
2174
[3373]2175  } else if ( strcmp(k, "--x11") == 0 ) {
2176#ifdef ROAR_HAVE_LIBX11
2177   reg_x11 = 1;
2178#else
2179   ROAR_ERR("No X11 support compiled in!");
2180   return 1;
2181#endif
2182
2183
[3039]2184  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
[4052]2185   _CKHAVEARGS(1);
[3039]2186   g_config->jumbo_mtu = atoi(argv[++i]);
2187
[60]2188  } else if ( strcmp(k, "-G") == 0 ) {
[4052]2189   _CKHAVEARGS(1);
[444]2190   sock_grp  = argv[++i];
2191  } else if ( strcmp(k, "-U") == 0 ) {
[4052]2192   _CKHAVEARGS(1);
[444]2193   sock_user = argv[++i];
[0]2194
[68]2195  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]2196#ifdef ROAR_SUPPORT_LISTEN
[2530]2197   sock_addr   = "";
[548]2198   g_terminate = 1;
[1155]2199   g_no_listen = 1;
[1494]2200#endif
[68]2201  } else if ( strcmp(k, "--client-fh") == 0 ) {
[4052]2202   _CKHAVEARGS(1);
[3737]2203   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
[68]2204    ROAR_ERR("main(*): Can not set client's fh");
2205    return 1;
2206   }
[501]2207  } else if ( strcmp(k, "--close-fh") == 0 ) {
[4052]2208   _CKHAVEARGS(1);
[1486]2209#ifdef ROAR_HAVE_IO_POSIX
[501]2210   close(atoi(argv[++i]));
[1486]2211#else
2212   i++;
2213   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2214#endif
[68]2215
[920]2216  } else if ( strcmp(k, "--standby") == 0 ) {
2217   g_standby = 1;
2218  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2219   g_autostandby = 1;
[0]2220  } else {
2221   usage();
2222   return 1;
2223  }
2224
2225 }
[1503]2226#endif
[0]2227
[4418]2228#ifndef DEBUG
2229 // notify dbg if requested, if in DEBUG mode
2230 // do not able because it got enabled early.
2231 if ( g_verbose >= 4 )
2232  dbg_notify_cb_register();
2233#endif
2234
[4358]2235#ifndef ROAR_WITHOUT_DCOMP_MIXER
2236 if ( m_drv != NULL ) {
2237  if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) {
2238   ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv);
2239  }
2240 }
2241#endif
2242
[2485]2243#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]2244 if ( s_dev != NULL ) {
2245  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2246   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2247  }
2248 }
[2485]2249#endif
[1110]2250
[4141]2251 add_default_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]2252
[4055]2253 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
[0]2254
[2945]2255 if ( waveform_init() == -1 ) {
2256  ROAR_ERR("Can not initialize Waveform subsystem");
2257  return 1;
2258 }
2259
[2500]2260#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]2261 if ( midi_init() == -1 ) {
[281]2262  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]2263 }
[2500]2264#endif
[1819]2265
[2489]2266#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2267 if ( ssynth_init() == -1 ) {
2268  ROAR_ERR("Can not initialize ssynth subsystem");
2269 }
[2489]2270#endif
[2451]2271
[2494]2272#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]2273 if ( light_init(light_channels) == -1 ) {
2274  ROAR_ERR("Can not initialize light control subsystem");
2275 }
[2494]2276#endif
[281]2277
[2718]2278#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2279 if ( rdtcs_init() == -1 ) {
2280  ROAR_ERR("Can not initialize RDTCS subsystem");
2281 }
2282#endif
2283
[3354]2284 if ( plugins_init() == -1 ) {
2285  ROAR_ERR("Can not initialize plugins");
2286 }
2287
[1494]2288#ifdef ROAR_SUPPORT_LISTEN
[3760]2289 if ( !g_no_listen ) {
2290  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2291   ROAR_ERR("Can not open listen socket!");
2292   return 1;
2293  }
[60]2294 }
[1494]2295#endif
[60]2296
[0]2297 if ( output_buffer_init(&sa) == -1 ) {
2298  ROAR_ERR("Can not init output buffer!");
2299  return 1;
2300 }
2301
[1145]2302 if ( driver == NULL ) {
2303  driver = "null";
2304 } else {
[4225]2305  ROAR_ERR("Usage of old driver interface. use -o not -d!");
2306  ROAR_WARN("-d will be removed within the next releases");
[1145]2307 }
2308
[0]2309 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
2310  ROAR_ERR("Can not open output driver!");
2311  return 1;
2312 }
2313
[44]2314 if ( samples_init() == -1 ) {
2315  ROAR_ERR("Can not init samples!");
2316  return 1;
2317 }
2318
2319
[1486]2320 // we should handle this on microcontrollers, too.
[1753]2321#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]2322 signal(SIGINT,  on_sig_int);
[2732]2323 signal(SIGTERM, on_sig_term);
[285]2324 signal(SIGCHLD, on_sig_chld);
[2112]2325 signal(SIGUSR1, on_sig_usr1);
[0]2326 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]2327#endif
[0]2328
[4245]2329 if ( g_config->memlock_level == -1 ) {
2330  g_config->memlock_level = MEMLOCK_DEFAULT;
2331 }
2332
2333 if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2334  ROAR_WARN("Can not set memory locking level to target level.");
2335 }
2336
[275]2337 if ( realtime ) {
[278]2338#ifdef DEBUG
[4246]2339  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
[278]2340#endif
2341
[1486]2342#ifdef ROAR_HAVE_NICE
[275]2343  errno = 0;
[276]2344  nice(-5*realtime); // -5 for each --realtime
[1486]2345  if ( errno ) {
2346   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2347  }
2348#else
2349  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2350#endif
[277]2351/*
[276]2352#ifdef __linux__
[277]2353  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2354   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]2355#endif
[277]2356*/
[275]2357 }
2358
[2756]2359#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[444]2360 if ( setids & R_SETGID ) {
[2754]2361  if ( sock_grp == NULL ) {
2362   ROAR_ERR("Can not set GID if no groupname is supplied");
2363   return 1;
2364  }
[2584]2365  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2366   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2367   return 1;
2368  }
[444]2369  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2370   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2371  }
[523]2372  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]2373   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2374  }
2375 }
[1486]2376#endif
[444]2377
[0]2378
[39]2379 clients_set_pid(g_self_client, getpid());
[1753]2380#ifdef ROAR_HAVE_GETUID
[440]2381 clients_set_uid(g_self_client, getuid());
[1753]2382#endif
2383#ifdef ROAR_HAVE_GETGID
[440]2384 clients_set_gid(g_self_client, getgid());
[1753]2385#endif
[39]2386 clients_get(g_self_client, &self);
[37]2387
[39]2388 if ( self == NULL ) {
2389  ROAR_ERR("Can not get self client!");
2390  return 1;
2391 }
2392
[775]2393 strcpy(self->name, "RoarAudio daemon internal");
[68]2394
[2815]2395 if ( roar_nnode_free(&(self->nnode)) == -1 )
2396  return 1;
2397
2398 // not fully correct but ok as workaorund
2399 // so tools assume that roard runs on the same machine as
2400 // they in case they use AF_UNIX:
2401 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2402  return 1;
2403
[1486]2404#ifdef ROAR_HAVE_FORK
[775]2405 if ( daemon ) {
[3613]2406#ifdef ROAR_HAVE_SYSLOG
2407  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2408#else
[3609]2409  roar_debug_set_stderr_fh(-1);
[3613]2410#endif
[3609]2411
[68]2412  close(ROAR_STDIN );
2413  close(ROAR_STDOUT);
2414  close(ROAR_STDERR);
[1753]2415
[68]2416  if ( fork() )
[1486]2417   ROAR_U_EXIT(0);
[1753]2418
2419#ifdef ROAR_HAVE_SETSID
2420  setsid();
2421#endif
[1046]2422  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]2423 }
[1486]2424#endif
[68]2425
[2757]2426#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2427 // early test for UID as we need this for the pidfile and the setuid()
2428 if ( sock_user != NULL ) {
2429  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2430   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2431   return 1;
2432  }
2433 }
2434#endif
2435
[2104]2436#ifdef SUPPORT_PIDFILE
[2106]2437 if ( pidfile != NULL ) {
2438  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
2439   ROAR_ERR("Can not write pidfile: %s", pidfile);
2440  } else {
2441   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2442   roar_vio_close(&pidfile_vio);
2443  }
[2765]2444#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[2759]2445  if ( pwd || grp ) {
2446   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
2447    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2448   }
2449  }
2450  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2451   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2452  }
[2765]2453#endif
[2104]2454 }
2455#endif
2456
[1486]2457#ifdef ROAR_HAVE_CHROOT
[444]2458 if (chrootdir) {
2459  if ( chroot(chrootdir) == -1 ) {
2460   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2461   return 2;
2462  }
2463  if ( chdir("/") == -1 ) {
2464   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2465   return 2;
2466  }
2467 }
[1486]2468#endif
[444]2469
[2756]2470#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[444]2471 if ( setids & R_SETUID ) {
[2752]2472  if ( sock_user == NULL ) {
2473   ROAR_ERR("Can not set UID if no username is supplied");
2474   return 1;
2475  }
[444]2476  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
2477   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2478   return 3;
2479  }
[1753]2480#ifdef ROAR_HAVE_GETUID
[444]2481  clients_set_uid(g_self_client, getuid());
[1753]2482#endif
[444]2483 }
[1486]2484#endif
[444]2485
[1993]2486 // Register with OpenSLP:
[2028]2487#ifdef ROAR_HAVE_LIBSLP
[1993]2488 if ( reg_slp ) {
[2530]2489  register_slp(0, sock_addr);
[1993]2490 }
[2028]2491#endif
[1993]2492
[3373]2493#ifdef ROAR_HAVE_LIBX11
2494 if ( reg_x11 ) {
2495  register_x11(0, sock_addr);
2496 }
2497#endif
2498
[4103]2499 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2500
[0]2501 // start main loop...
[4055]2502 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
[905]2503 main_loop(drvid, drvinst, &sa, sysclocksync);
[4055]2504 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
[0]2505
2506 // clean up.
2507 clean_quit_prep();
2508 driver_close(drvinst, drvid);
2509 output_buffer_free();
2510
[4323]2511 roar_notify_core_free(NULL);
2512
[4055]2513 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
[0]2514 return 0;
2515}
2516
[574]2517void cleanup_listen_socket (int terminate) {
[2530]2518 int i;
2519
[4055]2520 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2521
2522 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2523
[1993]2524 // Deregister from SLP:
[2028]2525#ifdef ROAR_HAVE_LIBSLP
[1993]2526 register_slp(1, NULL);
[2028]2527#endif
[580]2528
[3373]2529#ifdef ROAR_HAVE_LIBX11
2530 register_x11(1, NULL);
2531#endif
2532
[1494]2533#ifdef ROAR_SUPPORT_LISTEN
[2530]2534 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]2535  if ( g_listen[i].used  ) {
2536   roar_vio_close(&(g_listen[i].sock));
[60]2537
[3802]2538   g_listen[i].used = 0;
[576]2539
[1486]2540#ifdef ROAR_HAVE_UNIX
[2530]2541   if ( server[i] != NULL )
2542    if ( *(server[i]) == '/' )
2543     unlink(server[i]);
[1486]2544#endif
[2530]2545  }
[580]2546 }
[60]2547
[1494]2548#endif
2549
[574]2550 if ( terminate )
2551  g_terminate = 1;
2552}
2553
2554void clean_quit_prep (void) {
2555 cleanup_listen_socket(0);
[60]2556
[3354]2557 plugins_free();
2558
[2485]2559#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]2560 sources_free();
[2485]2561#endif
[0]2562 streams_free();
2563 clients_free();
[2489]2564#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2565 ssynth_free();
[2489]2566#endif
[2487]2567#ifndef ROAR_WITHOUT_DCOMP_CB
[282]2568 midi_cb_stop(); // stop console beep
[2487]2569#endif
[2500]2570#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]2571 midi_free();
[2500]2572#endif
[2494]2573#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]2574 light_free();
[2494]2575#endif
[2718]2576#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2577 rdtcs_free();
2578#endif
[2104]2579
[2945]2580 waveform_free();
2581
[2104]2582#ifdef SUPPORT_PIDFILE
2583 if ( pidfile != NULL )
2584  unlink(pidfile);
2585#endif
[0]2586}
2587
2588void clean_quit (void) {
[4055]2589 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2590
[4101]2591 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2592
[0]2593 clean_quit_prep();
2594// driver_close(drvinst, drvid);
2595// output_buffer_free();
[4055]2596
[4323]2597 roar_notify_core_free(NULL);
2598
[4055]2599 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
[0]2600 exit(0);
2601}
2602
2603//ll
Note: See TracBrowser for help on using the repository browser.