source: roaraudio/roard/roard.c @ 4382:0dfb01c8245c

Last change on this file since 4382:0dfb01c8245c was 4382:0dfb01c8245c, checked in by phi, 14 years ago

correctly set client for the mixer stream

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