source: roaraudio/libroaross/libroaross.c @ 3184:62cdc6ec7cca

Last change on this file since 3184:62cdc6ec7cca was 3184:62cdc6ec7cca, checked in by phi, 14 years ago

support to keep session open

File size: 24.0 KB
Line 
1//libroaross.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar 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 *  libroar 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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "roaraudio.h"
36
37#if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS)
38#if defined(__OpenBSD__) || defined(__NetBSD__)
39#include <soundcard.h>
40#else
41#include <sys/soundcard.h>
42#endif
43#include <sys/ioctl.h>
44
45#ifdef ROAR_HAVE_H_SYS_TYPES
46#include <sys/types.h>
47#endif
48
49#ifdef ROAR_HAVE_H_FCNTL
50#include <fcntl.h>
51#endif
52
53#ifdef ROAR_HAVE_H_UNISTD
54#include <unistd.h>
55#endif
56
57#include <sys/stat.h>
58#include <dlfcn.h>
59
60#if defined(RTLD_NEXT)
61#define REAL_LIBC RTLD_NEXT
62#else
63#define REAL_LIBC ((void *) -1L)
64#endif
65
66#ifndef ENOTSUP
67#define ENOTSUP ENOSYS
68#endif
69
70#if defined(ROAR_OS_NETBSD) && defined(ioctl)
71#define IOCTL_IS_ALIAS
72#endif
73
74#ifdef ROAR_OS_FREEBSD
75#define mode_t int
76#endif
77
78#ifdef ROAR_OS_NETBSD
79#define IOCTL() int _oss_ioctl __P((int fd, unsigned long com, void *argp))
80#define map_args int __fd = fd; unsigned long int __request = com
81#elif defined(ROAR_TARGET_CYGWIN)
82#define IOCTL() int ioctl (int __fd, int __cmd, ...)
83#define map_args unsigned long int __request = __cmd; void * argp
84#define va_argp
85#define ioctl_lastarg __cmd
86#else
87#define IOCTL() int ioctl (int __fd, unsigned long int __request, ...)
88#define map_args void * argp
89#define va_argp
90#define ioctl_lastarg __request
91#endif
92
93#define OSS_VOLUME_SCALE 100
94
95#define _MAX_POINTER  8
96
97// handle type:
98#define HT_NONE       0
99#define HT_STREAM     1
100#define HT_MIXER      2
101
102struct session {
103 int refc;
104 struct roar_connection con;
105};
106
107static struct session _session = {.refc = 0};
108
109struct handle {
110 int refc; // refrence counter
111 struct session * session;
112 int type;
113 struct roar_stream    stream;
114 struct roar_vio_calls stream_vio;
115 int                   stream_dir;
116 int                   stream_opened;
117 size_t                stream_buffersize;
118 size_t                readc, writec;
119};
120
121static struct {
122 int     (*open)(const char *pathname, int flags, mode_t mode);
123 int     (*close)(int fd);
124 ssize_t (*write)(int fd, const void *buf, size_t count);
125 ssize_t (*read)(int fd, void *buf, size_t count);
126#ifndef IOCTL_IS_ALIAS
127 int     (*ioctl)(int d, int request, ...);
128#endif
129} _os;
130
131static struct {
132 struct {
133  int volume;
134  int pcm;
135  int line;
136  int line1;
137  int line2;
138  int line3;
139  int digital1;
140  int digital2;
141  int digital3;
142 } sid;
143} _mix_settings = {
144                   .sid = {
145                           .volume   = -1,
146                           .pcm      = -1,
147                           .line     =  0,
148                           .line1    =  1,
149                           .line2    =  2,
150                           .line3    =  3,
151                           .digital1 =  1,
152                           .digital2 =  2,
153                           .digital3 =  3
154                          }
155                  };
156
157static struct pointer {
158 int fh;
159 struct handle * handle;
160} _ptr[_MAX_POINTER];
161
162static void _init_os (void) {
163 memset(&_os, 0, sizeof(_os));
164
165 _os.open  = dlsym(REAL_LIBC, "open");
166 _os.close = dlsym(REAL_LIBC, "close");
167 _os.write = dlsym(REAL_LIBC, "write");
168 _os.read  = dlsym(REAL_LIBC, "read");
169#ifndef IOCTL_IS_ALIAS
170 _os.ioctl = dlsym(REAL_LIBC, "ioctl");
171#endif
172}
173
174static void _init_ptr (void) {
175 int i;
176
177 for (i = 0; i < _MAX_POINTER; i++) {
178  _ptr[i].fh = -1;
179 }
180}
181
182static void _init (void) {
183 static int inited = 0;
184
185 if ( !inited ) {
186  _init_os();
187  _init_ptr();
188  inited++;
189 }
190}
191
192static void _find_volume_sid (struct session * session) {
193 int i;
194 int num;
195 int id[ROAR_STREAMS_MAX];
196 struct roar_stream s;
197 char name[1024];
198
199 ROAR_DBG("_find_volume_sid(session=%p) = ?", session);
200
201 if ( (num = roar_list_streams(&(session->con), id, ROAR_STREAMS_MAX)) == -1 ) {
202  return;
203 }
204
205 for (i = 0; i < num; i++) {
206  if ( roar_get_stream(&(session->con), &s, id[i]) == -1 )
207   continue;
208
209  if ( s.dir != ROAR_DIR_MIXING )
210   continue;
211
212  if ( roar_stream_get_name(&(session->con), &s, name, 1024) == -1 )
213   continue;
214
215  if ( !strcasecmp(name, "Waveform Mixer") ) {
216   _mix_settings.sid.volume = id[i];
217   ROAR_DBG("_find_volume_sid(session=%p): found waveform mixer at sid %i", session, id[i]);
218   ROAR_DBG("_find_volume_sid(session=%p) = (void)", session);
219   return;
220  }
221 }
222}
223
224static int _open_dummy (void) {
225 int p[2];
226
227 if ( pipe(p) == -1 )
228  return -1;
229
230 close(p[1]);
231
232 return p[0];
233}
234
235static struct session * _open_session (char * server, char * name) {
236 ROAR_DBG("_open_session(server='%s', name='%s') = ?", server, name);
237 ROAR_DBG("_open_session(server='%s', name='%s'): _session.refc=%i", server, name, _session.refc);
238
239 if ( _session.refc == 0 ) {
240
241  if ( name == NULL )
242   name = "libroaross client";
243
244  if ( roar_simple_connect(&(_session.con), server, name) == -1 )
245   return NULL;
246
247  _find_volume_sid(&_session);
248
249  if ( getenv("ROAR_OSS_KEEP_SESSION") != NULL )
250   _session.refc++;
251 }
252
253 _session.refc++;
254
255 ROAR_DBG("_open_session(server='%s', name='%s') = %p", server, name, &_session);
256 return &_session;
257}
258
259static void _close_session(struct session * session) {
260 if ( session == NULL )
261  return;
262
263 session->refc--;
264
265 ROAR_DBG("_close_session(session=%p): session->refc=%i", session, session->refc);
266
267 if ( session->refc == 0 ) {
268  roar_disconnect(&(session->con));
269 }
270}
271
272static struct handle * _open_handle(struct session * session) {
273 struct handle * handle;
274
275 ROAR_DBG("_open_handle(session=%p) = ?", session);
276
277 if ( (handle = roar_mm_malloc(sizeof(struct handle))) == NULL )
278  return NULL;
279
280 memset(handle, 0, sizeof(struct handle));
281
282 handle->refc = 1;
283 handle->session = session;
284 session->refc++; // TODO: better warp this
285 handle->type = HT_NONE;
286 handle->stream_dir = ROAR_DIR_PLAY;
287 roar_stream_new(&(handle->stream), ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, ROAR_CODEC_DEFAULT);
288
289 ROAR_DBG("_open_handle(session=%p) = %p", session, handle);
290 return handle;
291}
292
293static void _close_handle(struct handle * handle) {
294 if (handle == NULL)
295  return;
296
297 handle->refc--;
298
299 ROAR_DBG("_close_handle(handle=%p): handle->refc=%i", handle, handle->refc);
300
301 if ( handle->refc == 0 ) {
302  if ( handle->stream_opened )
303   roar_vio_close(&(handle->stream_vio));
304
305  handle->session->refc--;
306
307  _close_session(handle->session);
308
309  roar_mm_free(handle);
310 }
311}
312
313static struct pointer * _get_pointer_by_fh (int fh) {
314 int i;
315
316 for (i = 0; i < _MAX_POINTER; i++) {
317  if ( _ptr[i].fh == fh )
318   return &(_ptr[i]);
319 }
320
321 return NULL;
322}
323
324static struct pointer * _open_pointer(struct handle * handle) {
325 struct pointer * ret = _get_pointer_by_fh(-1);
326
327 if ( ret == NULL )
328  return NULL;
329
330 if ( (ret->fh = _open_dummy()) == -1 )
331  return NULL;
332
333 ret->handle = handle;
334
335 return ret;
336}
337
338static void _close_pointer(struct pointer * pointer) {
339 if ( pointer == NULL )
340  return;
341
342 _os.close(pointer->fh);
343
344 pointer->fh = -1;
345
346 _close_handle(pointer->handle);
347}
348
349// -------------------------------------
350// central open function:
351// -------------------------------------
352
353static int _open_file (const char *pathname, int flags) {
354 struct session * session;
355 struct handle  * handle;
356 struct pointer * pointer;
357 struct {
358  char * prefix;
359  int type;
360 } * ptr = NULL, p[] = {
361  {"/dev/dsp",   HT_STREAM},
362  {"/dev/audio", HT_STREAM},
363  {"/dev/mixer", HT_MIXER},
364#ifdef ROAR_DEFAULT_OSS_DEV
365  {ROAR_DEFAULT_OSS_DEV, HT_STREAM},
366#endif
367  {NULL, HT_NONE},
368 };
369 int i;
370
371 for (i = 0; p[i].prefix != NULL; i++) {
372  if ( !strcmp(pathname, p[i].prefix) ) {
373   ptr = &(p[i]);
374  }
375 }
376
377 if ( ptr == NULL )
378  return -2;
379
380 if ( (session = _open_session(NULL, NULL)) == NULL ) {
381  return -1;
382 }
383
384 if ( (handle = _open_handle(session)) == NULL ) {
385  _close_session(session);
386  return -1;
387 }
388
389 handle->type = ptr->type;
390
391 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
392  case O_RDONLY:
393    handle->stream_dir = ROAR_DIR_MONITOR;
394   break;
395  case O_WRONLY:
396    handle->stream_dir = ROAR_DIR_PLAY;
397   break;
398  case O_RDWR:
399    handle->stream_dir = ROAR_DIR_BIDIR;
400   break;
401 }
402
403 if ( (pointer = _open_pointer(handle)) == NULL ) {
404  _close_handle(handle);
405  return -1;
406 }
407
408 return pointer->fh;
409}
410
411// -------------------------------------
412// open function for streams:
413// -------------------------------------
414
415static int _open_stream (struct handle * handle) {
416  // FIXME: this should be re-written much more cleanly:
417
418 if ( handle == NULL )
419  return -1;
420
421 if ( roar_vio_simple_new_stream_obj(&(handle->stream_vio),
422                                     &(handle->session->con), &(handle->stream),
423                                     handle->stream.info.rate,
424                                     handle->stream.info.channels,
425                                     handle->stream.info.bits,
426                                     handle->stream.info.codec,
427                                     handle->stream_dir
428                                    ) == -1 )
429  return -1;
430
431 handle->stream_opened++;
432
433 _mix_settings.sid.pcm = roar_stream_get_id(&(handle->stream));
434
435 return 0;
436}
437
438// -------------------------------------
439// function to parse format:
440// -------------------------------------
441
442static int _ioctl_stream_format (struct handle * handle, int format) {
443 struct roar_audio_info * info = &(handle->stream.info);
444
445 switch (format) {
446  case AFMT_S8:
447    info->bits  = 8;
448    info->codec = ROAR_CODEC_PCM_S_LE;
449   break;
450  case AFMT_U8:
451    info->bits  = 8;
452    info->codec = ROAR_CODEC_PCM_U_LE;
453   break;
454  case AFMT_S16_BE:
455    info->bits  = 16;
456    info->codec = ROAR_CODEC_PCM_S_BE;
457   break;
458  case AFMT_S16_LE:
459    info->bits  = 16;
460    info->codec = ROAR_CODEC_PCM_S_LE;
461   break;
462  case AFMT_U16_BE:
463    info->bits  = 16;
464    info->codec = ROAR_CODEC_PCM_U_BE;
465   break;
466  case AFMT_U16_LE:
467    info->bits  = 16;
468    info->codec = ROAR_CODEC_PCM_U_LE;
469   break;
470#ifdef AFMT_S32_BE
471  case AFMT_S32_BE:
472    info->bits  = 32;
473    info->codec = ROAR_CODEC_PCM_S_BE;
474   break;
475#endif
476#ifdef AFMT_S32_LE
477  case AFMT_S32_LE:
478    info->bits  = 32;
479    info->codec = ROAR_CODEC_PCM_S_LE;
480   break;
481#endif
482  case AFMT_A_LAW:
483    info->bits  = 8;
484    info->codec = ROAR_CODEC_ALAW;
485   break;
486  case AFMT_MU_LAW:
487    info->bits  = 8;
488    info->codec = ROAR_CODEC_MULAW;
489   break;
490#ifdef AFMT_VORBIS
491  case AFMT_VORBIS:
492    info->codec = ROAR_CODEC_OGG_VORBIS;
493   break;
494#endif
495  default:
496    errno = ENOSYS;
497    return -1;
498   break;
499 }
500
501 return 0;
502}
503
504static inline int _ioctl_stream_format_list (void) {
505 int format = 0;
506
507 format |= AFMT_S8;
508 format |= AFMT_U8;
509
510 format |= AFMT_S16_BE;
511 format |= AFMT_S16_LE;
512
513 format |= AFMT_U16_BE;
514 format |= AFMT_U16_LE;
515
516#ifdef AFMT_S32_BE
517 format |= AFMT_S32_BE;
518#endif
519#ifdef AFMT_S32_LE
520 format |= AFMT_S32_LE;
521#endif
522
523 format |= AFMT_A_LAW;
524 format |= AFMT_MU_LAW;
525
526#ifdef AFMT_VORBIS
527 format |= AFMT_VORBIS;
528#endif
529
530 return format;
531}
532
533// -------------------------------------
534// mixer ioctls:
535// -------------------------------------
536
537static int _ioctl_mixer (struct handle * handle, long unsigned int req, void * vp) {
538 mixer_info * info;
539 int channels;
540 struct roar_mixer_settings mixer;
541 int o_w    =  0;
542 int o_sid  = -1;
543 int * ip   = vp;
544#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
545 char * name = NULL;
546#endif
547
548#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
549 switch (req) {
550#if 0
551  case SNDCTL_MIX_DESCRIPTION: name = "SNDCTL_MIX_DESCRIPTION"; break;
552  case SNDCTL_MIX_ENUMINFO:    name = "SNDCTL_MIX_ENUMINFO";    break;
553  case SNDCTL_MIX_EXTINFO:     name = "SNDCTL_MIX_EXTINFO";     break;
554  case SNDCTL_MIX_NREXT:       name = "SNDCTL_MIX_NREXT";       break;
555  case SNDCTL_MIX_NRMIX:       name = "SNDCTL_MIX_NRMIX";       break;
556  case SNDCTL_MIX_READ:        name = "SNDCTL_MIX_READ";        break;
557  case SNDCTL_MIX_WRITE:       name = "SNDCTL_MIX_WRITE";       break;
558#endif
559//  case SOUND_MIXER_INFO:             name = "SOUND_MIXER_INFO";             break;
560  case SOUND_OLD_MIXER_INFO:         name = "SOUND_OLD_MIXER_INFO";         break;
561  case SOUND_MIXER_ACCESS:           name = "SOUND_MIXER_ACCESS";           break;
562  case SOUND_MIXER_AGC:              name = "SOUND_MIXER_AGC";              break;
563  case SOUND_MIXER_3DSE:             name = "SOUND_MIXER_3DSE";             break;
564  case SOUND_MIXER_GETLEVELS:        name = "SOUND_MIXER_GETLEVELS";        break;
565  case SOUND_MIXER_SETLEVELS:        name = "SOUND_MIXER_SETLEVELS";        break;
566  case SOUND_MIXER_PRIVATE1:         name = "SOUND_MIXER_PRIVATE1";         break;
567  case SOUND_MIXER_PRIVATE2:         name = "SOUND_MIXER_PRIVATE2";         break;
568  case SOUND_MIXER_PRIVATE3:         name = "SOUND_MIXER_PRIVATE3";         break;
569  case SOUND_MIXER_PRIVATE4:         name = "SOUND_MIXER_PRIVATE4";         break;
570  case SOUND_MIXER_PRIVATE5:         name = "SOUND_MIXER_PRIVATE5";         break;
571  case OSS_GETVERSION:               name = "OSS_GETVERSION";               break;
572//  case SOUND_MIXER_READ_CAPS:        name = "SOUND_MIXER_READ_CAPS";        break;
573  case SOUND_MIXER_READ_MUTE:        name = "SOUND_MIXER_READ_MUTE";        break;
574/*
575  case :     name = "";     break;
576  case :     name = "";     break;
577*/
578 }
579 if ( name != NULL ) {
580  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unspported mixer command %s", handle, req, ip, name);
581  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
582  errno = ENOSYS;
583  return -1;
584 }
585#endif
586
587 switch (req) {
588  case SOUND_MIXER_READ_VOLUME:    o_w = 0; o_sid = _mix_settings.sid.volume;   break;
589  case SOUND_MIXER_READ_LINE:      o_w = 0; o_sid = _mix_settings.sid.line;     break;
590  case SOUND_MIXER_READ_LINE1:     o_w = 0; o_sid = _mix_settings.sid.line1;    break;
591  case SOUND_MIXER_READ_LINE2:     o_w = 0; o_sid = _mix_settings.sid.line2;    break;
592  case SOUND_MIXER_READ_LINE3:     o_w = 0; o_sid = _mix_settings.sid.line3;    break;
593#if 0
594  case SOUND_MIXER_READ_DIGITAL1:  o_w = 0; o_sid = _mix_settings.sid.digital1; break;
595  case SOUND_MIXER_READ_DIGITAL2:  o_w = 0; o_sid = _mix_settings.sid.digital2; break;
596  case SOUND_MIXER_READ_DIGITAL3:  o_w = 0; o_sid = _mix_settings.sid.digital3; break;
597#endif
598  case SOUND_MIXER_WRITE_VOLUME:   o_w = 1; o_sid = _mix_settings.sid.volume;   break;
599  case SOUND_MIXER_WRITE_LINE:     o_w = 1; o_sid = _mix_settings.sid.line;     break;
600  case SOUND_MIXER_WRITE_LINE1:    o_w = 1; o_sid = _mix_settings.sid.line1;    break;
601  case SOUND_MIXER_WRITE_LINE2:    o_w = 1; o_sid = _mix_settings.sid.line2;    break;
602  case SOUND_MIXER_WRITE_LINE3:    o_w = 1; o_sid = _mix_settings.sid.line3;    break;
603#if 0
604  case SOUND_MIXER_WRITE_DIGITAL1: o_w = 1; o_sid = _mix_settings.sid.digital1; break;
605  case SOUND_MIXER_WRITE_DIGITAL2: o_w = 1; o_sid = _mix_settings.sid.digital2; break;
606  case SOUND_MIXER_WRITE_DIGITAL3: o_w = 1; o_sid = _mix_settings.sid.digital3; break;
607#endif
608  // we handle PCM seperatly as we want to be abled to abled to handle it on a stream (not mixer), too:
609  case SOUND_MIXER_READ_PCM:
610    o_w = 0;
611    if ( handle->type == HT_STREAM ) {
612     o_sid = roar_stream_get_id(&(handle->stream));
613    } else {
614     o_sid = _mix_settings.sid.pcm;
615    }
616   break;
617  case SOUND_MIXER_WRITE_PCM:
618    o_w = 1;
619    if ( handle->type == HT_STREAM ) {
620     o_sid = roar_stream_get_id(&(handle->stream));
621    } else {
622     o_sid = _mix_settings.sid.pcm;
623    }
624   break;
625 }
626 if ( o_sid != -1 ) {
627  // set/get volume
628  if ( o_w ) {
629   mixer.scale    = 65535;
630   mixer.mixer[0] = ( *ip       & 0xFF)*65535/OSS_VOLUME_SCALE;
631   mixer.mixer[1] = ((*ip >> 8) & 0xFF)*65535/OSS_VOLUME_SCALE;
632   if ( roar_set_vol(&(handle->session->con), o_sid, &mixer, 2) == -1 ) {
633    errno = EIO;
634    return -1;
635   }
636   return 0;
637  } else {
638   if ( roar_get_vol(&(handle->session->con), o_sid, &mixer, &channels) == -1 ) {
639    errno = EIO;
640    return -1;
641   }
642   *ip = ((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale) | (((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale)<<8);
643   return 0;
644  }
645 }
646
647 switch (req) {
648  case SOUND_MIXER_READ_STEREODEVS: /* FIXME: check the streams for channel config */
649  case SOUND_MIXER_READ_DEVMASK:
650    *ip = 0;
651
652    if ( _mix_settings.sid.volume != -1 )
653     *ip |= SOUND_MASK_VOLUME;
654    if ( _mix_settings.sid.pcm != -1 )
655     *ip |= SOUND_MASK_PCM;
656    if ( _mix_settings.sid.line != -1 )
657     *ip |= SOUND_MASK_LINE;
658    if ( _mix_settings.sid.line1 != -1 )
659     *ip |= SOUND_MASK_LINE1;
660    if ( _mix_settings.sid.line2 != -1 )
661     *ip |= SOUND_MASK_LINE2;
662    if ( _mix_settings.sid.line3 != -1 )
663     *ip |= SOUND_MASK_LINE3;
664    if ( _mix_settings.sid.digital1 != -1 )
665#if 0
666     *ip |= SOUND_MASK_DIGITAL1;
667    if ( _mix_settings.sid.digital2 != -1 )
668     *ip |= SOUND_MASK_DIGITAL2;
669    if ( _mix_settings.sid.digital3 != -1 )
670     *ip |= SOUND_MASK_DIGITAL3;
671#endif
672
673    return 0;
674   break;
675  case SOUND_MIXER_READ_RECMASK:
676  case SOUND_MIXER_READ_RECSRC:
677    *ip = SOUND_MASK_VOLUME; // we can currently only read from mixer
678    return 0;
679   break;
680  case SOUND_MIXER_WRITE_RECSRC:
681    if ( *ip == SOUND_MASK_VOLUME ) {
682     return  0;
683    } else {
684     errno = ENOTSUP;
685     return -1;
686    }
687   break;
688  case SOUND_MIXER_READ_CAPS:
689    *ip = 0;
690    return 0;
691   break;
692  case SOUND_MIXER_INFO:
693    info = vp;
694    memset(info, 0, sizeof(*info));
695    strcpy(info->id, "RoarAudio");
696    strcpy(info->name, "RoarAudio");
697    return 0;
698   break;
699 }
700
701 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unknown mixer CTL", handle, req, ip);
702// _os.ioctl(-1, req, ip);
703 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
704 errno = ENOSYS;
705 return -1;
706}
707
708// -------------------------------------
709// buffer size calculation:
710// -------------------------------------
711
712static size_t _get_stream_buffersize (struct handle * handle) {
713 if ( handle->stream_buffersize )
714  return handle->stream_buffersize;
715
716 return handle->stream_buffersize = handle->stream.info.rate     *
717                                    handle->stream.info.channels *
718                                    handle->stream.info.bits     / 800;
719}
720
721// -------------------------------------
722// emulated functions follow:
723// -------------------------------------
724
725int     open(const char *pathname, int flags, ...) {
726 int     ret;
727 mode_t  mode = 0;
728 va_list args;
729
730 _init();
731
732 ret = _open_file(pathname, flags);
733
734 switch (ret) {
735  case -2:       // continue as normal, use _op.open()
736   break;
737  case -1:       // pass error to caller
738    return -1;
739   break;
740  default:       // return successfully opened pointer to caller
741    return ret;
742   break;
743 }
744
745 if (flags & O_CREAT) {
746  va_start(args, flags);
747  mode = va_arg(args, mode_t);
748  va_end(args);
749 }
750
751 return _os.open(pathname, flags, mode);
752}
753
754int     close(int fd) {
755 struct pointer * pointer;
756 _init();
757
758 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
759  _close_pointer(pointer);
760  return 0;
761 }
762
763 return _os.close(fd);
764}
765
766ssize_t write(int fd, const void *buf, size_t count) {
767 struct pointer * pointer;
768 ssize_t ret;
769
770 _init();
771
772 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
773  if ( pointer->handle->type == HT_STREAM ) {
774   if ( pointer->handle->stream_opened == 0 ) {
775    if ( _open_stream(pointer->handle) == -1 ) {
776     errno = EIO;
777     return -1;
778    }
779   }
780   ret = roar_vio_write(&(pointer->handle->stream_vio), (char*)buf, count);
781   if ( ret > 0 )
782    pointer->handle->writec += ret;
783   return ret;
784  } else {
785   errno = EINVAL;
786   return -1;
787  }
788 }
789
790 return _os.write(fd, buf, count);
791}
792
793ssize_t read(int fd, void *buf, size_t count) {
794 struct pointer * pointer;
795 ssize_t ret;
796
797 _init();
798
799 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
800  if ( pointer->handle->type == HT_STREAM ) {
801   if ( pointer->handle->stream_opened == 0 ) {
802    if ( _open_stream(pointer->handle) == -1 ) {
803     errno = EIO;
804     return -1;
805    }
806   }
807   ret = roar_vio_read(&(pointer->handle->stream_vio), buf, count);
808   if ( ret > 0 )
809    pointer->handle->writec += ret;
810   return ret;
811  } else {
812   errno = EINVAL;
813   return -1;
814  }
815 }
816
817 return _os.read(fd, buf, count);
818}
819
820IOCTL() {
821 map_args;
822 struct pointer * pointer;
823 struct handle  * handle;
824 int * ip = NULL;
825 audio_buf_info * bi;
826 count_info     * ci;
827#ifdef va_argp
828 va_list args;
829#endif
830
831 _init();
832
833// ROAR_DBG("ioctl(__fd=%i, __request=%lu) = ?", __fd, (long unsigned int) __request);
834
835#ifdef va_argp
836 va_start (args, ioctl_lastarg);
837 argp = va_arg (args, void *);
838 va_end (args);
839#endif
840
841// ROAR_DBG("ioctl(__fd=%i, __request=%lu): argp=%p", __fd, (long unsigned int) __request, argp);
842
843 if ( (pointer = _get_pointer_by_fh(__fd)) != NULL ) {
844  ip = argp;
845//  ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
846  switch ((handle = pointer->handle)->type) {
847   case HT_STREAM:
848     switch (__request) {
849      case SNDCTL_DSP_RESET:
850      case SNDCTL_DSP_POST:
851      case SNDCTL_DSP_SETFRAGMENT: // any fragments should be ok for us...
852        return 0;
853       break;
854      case SNDCTL_DSP_SPEED:
855        handle->stream.info.rate = *ip;
856        return 0;
857       break;
858      case SNDCTL_DSP_CHANNELS:
859        handle->stream.info.channels = *ip;
860        return 0;
861       break;
862      case SNDCTL_DSP_STEREO:
863        handle->stream.info.channels = *ip ? 2 : 1;
864        return 0;
865       break;
866      case SNDCTL_DSP_GETBLKSIZE:
867        *ip = _get_stream_buffersize(handle);
868        return 0;
869       break;
870      case SNDCTL_DSP_SETFMT:
871        return _ioctl_stream_format(handle, *ip);
872       break;
873      case SNDCTL_DSP_GETFMTS:
874//        ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
875        *ip = _ioctl_stream_format_list();
876        return 0;
877       break;
878      case SNDCTL_DSP_GETOSPACE:
879      case SNDCTL_DSP_GETISPACE:
880        bi = argp;
881        memset(bi, 0, sizeof(*bi));
882        bi->bytes      = _get_stream_buffersize(handle);
883        bi->fragments  = 1;
884        bi->fragsize   = bi->bytes;
885        bi->fragstotal = 1;
886        return 0;
887       break;
888      case SNDCTL_DSP_GETOPTR:
889        ci = argp;
890        memset(ci, 0, sizeof(*ci));
891        ci->bytes  = handle->writec;
892        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
893        ci->ptr    = 0;
894        return 0;
895       break;
896      case SNDCTL_DSP_GETIPTR:
897        ci = argp;
898        memset(ci, 0, sizeof(*ci));
899        ci->bytes  = handle->readc;
900        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
901        ci->ptr    = 0;
902        return 0;
903       break;
904#ifdef SNDCTL_DSP_GETPLAYVOL
905      case SNDCTL_DSP_GETPLAYVOL:
906        return _ioctl_mixer(handle, SOUND_MIXER_READ_PCM, argp);
907       break;
908#endif
909#ifdef SNDCTL_DSP_SETPLAYVOL
910      case SNDCTL_DSP_SETPLAYVOL:
911        return _ioctl_mixer(handle, SOUND_MIXER_WRITE_PCM, argp);
912       break;
913#endif
914      default:
915        ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
916        errno = ENOSYS;
917        return -1;
918     }
919    break;
920   case HT_MIXER:
921     return _ioctl_mixer(handle, __request, argp);
922    break;
923   default:
924     ROAR_DBG("ioctl(__fd=%i, __request=%lu): unknown handle type: no ioctl()s supported", __fd, (long unsigned int) __request);
925     ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
926     errno = EINVAL;
927     return -1;
928    break;
929  }
930 }
931
932#ifdef IOCTL_IS_ALIAS
933 errno = ENOSYS;
934 return -1;
935#else
936 return _os.ioctl(__fd, __request, argp);
937#endif
938}
939
940#endif
941
942//ll
Note: See TracBrowser for help on using the repository browser.