source: roaraudio/roard/roard.c @ 4345:7193b53f6f8d

Last change on this file since 4345:7193b53f6f8d was 4345:7193b53f6f8d, checked in by phi, 14 years ago

support command names

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