source: roaraudio/libroarsndio/libroarsndio.c @ 5754:b23d79c13370

Last change on this file since 5754:b23d79c13370 was 5754:b23d79c13370, checked in by phi, 11 years ago

avoid getenv() and use more portable roar_env_get(). Also improved security as we enfore const now on bufferes obtained from the env.

File size: 5.4 KB
Line 
1//libroarsndio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from OpenBSD's sndio.
7 *  See 'Copyright for sndio' below for more information on
8 *  code fragments taken from OpenBSD's sndio.
9 *
10 * --- Copyright for sndio ---
11 * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
12 *
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * --- End of Copyright for sndio ---
25 *
26 *  This file is part of libroaresd a part of RoarAudio,
27 *  a cross-platform sound system for both, home and professional use.
28 *  See README for details.
29 *
30 *  This file is free software; you can redistribute it and/or modify
31 *  it under the terms of the GNU General Public License version 3
32 *  as published by the Free Software Foundation.
33 *
34 *  RoarAudio is distributed in the hope that it will be useful,
35 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
36 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 *  GNU General Public License for more details.
38 *
39 *  You should have received a copy of the GNU General Public License
40 *  along with this software; see the file COPYING.  If not, write to
41 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
42 *  Boston, MA 02110-1301, USA.
43 *
44 *  NOTE for everyone want's to change something and send patches:
45 *  read README and HACKING! There a addition information on
46 *  the license of this document you need to read before you send
47 *  any patches.
48 */
49
50#define ROAR_USE_OWN_SNDIO_HDL
51#include "libroarsndio.h"
52
53static char * sndio_to_roar_names (char * name) {
54 char * unitoffset = NULL;
55 char * optsoffset = NULL;
56 int    unit;
57
58 if ( name == NULL )
59  return NULL;
60
61 if ( !strncmp(name, "sun:", 4) ) {
62  unitoffset = name + 4;
63 } else if ( !strncmp(name, "aucat:", 6) ) {
64  unitoffset = name + 6;
65 } else if ( !strncmp(name, "rmidi:", 6) ) {
66  unitoffset = name + 6;
67 } else if ( !strncmp(name, "midithru:", 9) ) {
68  unitoffset = name + 9;
69 } else {
70  return name;
71 }
72
73 if ( (optsoffset = strstr(unitoffset, "/")) != NULL ) {
74  *optsoffset = 0;
75  return unitoffset;
76 } else if ( (optsoffset = strstr(unitoffset, ".")) != NULL ) {
77  // TODO: add some code to strip the options of the end
78  return name;
79 } else {
80  unit = atoi(unitoffset);
81  switch (unit) {
82   // use defaulst
83   case 0: return NULL; break;
84   // use UNIX user sock, TODO: need some code here...
85   case 1: return NULL; break;
86   // use UNIX global sock:
87   case 2: return ROAR_DEFAULT_SOCK_GLOBAL; break;
88   // use DECnet localnode:
89   case 3: return "::"; break;
90   // use IPv4 localhost:
91   case 4: return ROAR_DEFAULT_INET4_HOST; break;
92   // reserved for DECnet Phase V:
93   case 5: return name; break;
94   // use IPv6 localhost:
95   case 6: return ROAR_DEFAULT_INET6_HOST; break;
96   // default:
97   default:
98     return name;
99  }
100 }
101
102 return name;
103}
104
105struct sio_hdl * sio_open(const char * name, unsigned mode, int nbio_flag) {
106 struct sio_hdl * hdl = NULL;
107 int is_midi = 0;
108 char * tmp;
109
110 if ( (hdl = roar_mm_malloc(sizeof(struct sio_hdl))) == NULL )
111  return NULL;
112
113 memset(hdl, 0, sizeof(struct sio_hdl));
114 hdl->device = NULL;
115
116 switch (mode) {
117  case SIO_PLAY:
118    hdl->dir = ROAR_DIR_PLAY;
119   break;
120  case SIO_REC:
121    hdl->dir = ROAR_DIR_PLAY;
122   break;
123  case MIO_OUT:
124    is_midi = 1;
125    hdl->dir = ROAR_DIR_MIDI_IN;
126   break;
127  case MIO_IN:
128    is_midi = 1;
129    hdl->dir = ROAR_DIR_MIDI_OUT;
130   break;
131
132  // unsupported:
133  case SIO_PLAY|SIO_REC:
134  case MIO_OUT|MIO_IN:
135
136  // illigal:
137  default:
138    roar_mm_free(hdl);
139    return NULL;
140 }
141
142 if ( name == NULL ) {
143  if ( is_midi ) {
144   name = roar_env_get("MIDIDEVICE");
145  } else {
146   name = roar_env_get("AUDIODEVICE");
147  }
148 }
149
150 if ( name != NULL ) {
151  tmp = roar_mm_strdup(name);
152  name = sndio_to_roar_names(tmp);
153
154  if ( name != NULL )
155   hdl->device = roar_mm_strdup(name);
156
157  roar_mm_free(tmp);
158 }
159
160 if ( roar_simple_connect(&(hdl->con), hdl->device, "libroarsndio") == -1 ) {
161  roar_mm_free(hdl->device);
162  roar_mm_free(hdl);
163  return NULL;
164 }
165
166 sio_initpar(&(hdl->para));
167
168 hdl->stream_opened = 0;
169
170 if ( is_midi ) {
171  hdl->info.codec    = ROAR_CODEC_MIDI;
172  hdl->info.bits     = ROAR_MIDI_BITS;
173  hdl->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
174  hdl->info.rate     = ROAR_MIDI_TICKS_PER_BEAT;
175  if ( !sio_start(hdl) ) {
176   sio_close(hdl);
177   return NULL;
178  }
179 }
180
181 hdl->nonblock = nbio_flag;
182
183 return hdl;
184}
185
186void   sio_close  (struct sio_hdl * hdl) {
187 if ( hdl == NULL )
188  return;
189
190 sio_stop(hdl);
191
192 roar_disconnect(&(hdl->con));
193
194 if ( hdl->device != NULL )
195  roar_mm_free(hdl->device);
196 roar_mm_free(hdl);
197}
198
199int    sio_eof    (struct sio_hdl * hdl) {
200 return hdl->ioerror;
201}
202
203void   sio_onmove (struct sio_hdl * hdl, void (*cb)(void * arg, int delta), void * arg) {
204 if ( hdl == NULL )
205  return;
206
207 hdl->on_move     = cb;
208 hdl->on_move_arg = arg;
209}
210
211//ll
Note: See TracBrowser for help on using the repository browser.