source: roaraudio/libroaross/libroaross.c @ 3183:a965fad9c167

Last change on this file since 3183:a965fad9c167 was 3183:a965fad9c167, checked in by phi, 14 years ago

more debug lions

File size: 23.9 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
250 _session.refc++;
251
252 ROAR_DBG("_open_session(server='%s', name='%s') = %p", server, name, &_session);
253 return &_session;
254}
255
256static void _close_session(struct session * session) {
257 if ( session == NULL )
258  return;
259
260 session->refc--;
261
262 ROAR_DBG("_close_session(session=%p): session->refc=%i", session, session->refc);
263
264 if ( session->refc == 0 ) {
265  roar_disconnect(&(session->con));
266 }
267}
268
269static struct handle * _open_handle(struct session * session) {
270 struct handle * handle;
271
272 ROAR_DBG("_open_handle(session=%p) = ?", session);
273
274 if ( (handle = roar_mm_malloc(sizeof(struct handle))) == NULL )
275  return NULL;
276
277 memset(handle, 0, sizeof(struct handle));
278
279 handle->refc = 1;
280 handle->session = session;
281 session->refc++; // TODO: better warp this
282 handle->type = HT_NONE;
283 handle->stream_dir = ROAR_DIR_PLAY;
284 roar_stream_new(&(handle->stream), ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, ROAR_CODEC_DEFAULT);
285
286 ROAR_DBG("_open_handle(session=%p) = %p", session, handle);
287 return handle;
288}
289
290static void _close_handle(struct handle * handle) {
291 if (handle == NULL)
292  return;
293
294 handle->refc--;
295
296 ROAR_DBG("_close_handle(handle=%p): handle->refc=%i", handle, handle->refc);
297
298 if ( handle->refc == 0 ) {
299  if ( handle->stream_opened )
300   roar_vio_close(&(handle->stream_vio));
301
302  handle->session->refc--;
303
304  _close_session(handle->session);
305
306  roar_mm_free(handle);
307 }
308}
309
310static struct pointer * _get_pointer_by_fh (int fh) {
311 int i;
312
313 for (i = 0; i < _MAX_POINTER; i++) {
314  if ( _ptr[i].fh == fh )
315   return &(_ptr[i]);
316 }
317
318 return NULL;
319}
320
321static struct pointer * _open_pointer(struct handle * handle) {
322 struct pointer * ret = _get_pointer_by_fh(-1);
323
324 if ( ret == NULL )
325  return NULL;
326
327 if ( (ret->fh = _open_dummy()) == -1 )
328  return NULL;
329
330 ret->handle = handle;
331
332 return ret;
333}
334
335static void _close_pointer(struct pointer * pointer) {
336 if ( pointer == NULL )
337  return;
338
339 _os.close(pointer->fh);
340
341 pointer->fh = -1;
342
343 _close_handle(pointer->handle);
344}
345
346// -------------------------------------
347// central open function:
348// -------------------------------------
349
350static int _open_file (const char *pathname, int flags) {
351 struct session * session;
352 struct handle  * handle;
353 struct pointer * pointer;
354 struct {
355  char * prefix;
356  int type;
357 } * ptr = NULL, p[] = {
358  {"/dev/dsp",   HT_STREAM},
359  {"/dev/audio", HT_STREAM},
360  {"/dev/mixer", HT_MIXER},
361#ifdef ROAR_DEFAULT_OSS_DEV
362  {ROAR_DEFAULT_OSS_DEV, HT_STREAM},
363#endif
364  {NULL, HT_NONE},
365 };
366 int i;
367
368 for (i = 0; p[i].prefix != NULL; i++) {
369  if ( !strcmp(pathname, p[i].prefix) ) {
370   ptr = &(p[i]);
371  }
372 }
373
374 if ( ptr == NULL )
375  return -2;
376
377 if ( (session = _open_session(NULL, NULL)) == NULL ) {
378  return -1;
379 }
380
381 if ( (handle = _open_handle(session)) == NULL ) {
382  _close_session(session);
383  return -1;
384 }
385
386 handle->type = ptr->type;
387
388 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
389  case O_RDONLY:
390    handle->stream_dir = ROAR_DIR_MONITOR;
391   break;
392  case O_WRONLY:
393    handle->stream_dir = ROAR_DIR_PLAY;
394   break;
395  case O_RDWR:
396    handle->stream_dir = ROAR_DIR_BIDIR;
397   break;
398 }
399
400 if ( (pointer = _open_pointer(handle)) == NULL ) {
401  _close_handle(handle);
402  return -1;
403 }
404
405 return pointer->fh;
406}
407
408// -------------------------------------
409// open function for streams:
410// -------------------------------------
411
412static int _open_stream (struct handle * handle) {
413  // FIXME: this should be re-written much more cleanly:
414
415 if ( handle == NULL )
416  return -1;
417
418 if ( roar_vio_simple_new_stream_obj(&(handle->stream_vio),
419                                     &(handle->session->con), &(handle->stream),
420                                     handle->stream.info.rate,
421                                     handle->stream.info.channels,
422                                     handle->stream.info.bits,
423                                     handle->stream.info.codec,
424                                     handle->stream_dir
425                                    ) == -1 )
426  return -1;
427
428 handle->stream_opened++;
429
430 _mix_settings.sid.pcm = roar_stream_get_id(&(handle->stream));
431
432 return 0;
433}
434
435// -------------------------------------
436// function to parse format:
437// -------------------------------------
438
439static int _ioctl_stream_format (struct handle * handle, int format) {
440 struct roar_audio_info * info = &(handle->stream.info);
441
442 switch (format) {
443  case AFMT_S8:
444    info->bits  = 8;
445    info->codec = ROAR_CODEC_PCM_S_LE;
446   break;
447  case AFMT_U8:
448    info->bits  = 8;
449    info->codec = ROAR_CODEC_PCM_U_LE;
450   break;
451  case AFMT_S16_BE:
452    info->bits  = 16;
453    info->codec = ROAR_CODEC_PCM_S_BE;
454   break;
455  case AFMT_S16_LE:
456    info->bits  = 16;
457    info->codec = ROAR_CODEC_PCM_S_LE;
458   break;
459  case AFMT_U16_BE:
460    info->bits  = 16;
461    info->codec = ROAR_CODEC_PCM_U_BE;
462   break;
463  case AFMT_U16_LE:
464    info->bits  = 16;
465    info->codec = ROAR_CODEC_PCM_U_LE;
466   break;
467#ifdef AFMT_S32_BE
468  case AFMT_S32_BE:
469    info->bits  = 32;
470    info->codec = ROAR_CODEC_PCM_S_BE;
471   break;
472#endif
473#ifdef AFMT_S32_LE
474  case AFMT_S32_LE:
475    info->bits  = 32;
476    info->codec = ROAR_CODEC_PCM_S_LE;
477   break;
478#endif
479  case AFMT_A_LAW:
480    info->bits  = 8;
481    info->codec = ROAR_CODEC_ALAW;
482   break;
483  case AFMT_MU_LAW:
484    info->bits  = 8;
485    info->codec = ROAR_CODEC_MULAW;
486   break;
487#ifdef AFMT_VORBIS
488  case AFMT_VORBIS:
489    info->codec = ROAR_CODEC_OGG_VORBIS;
490   break;
491#endif
492  default:
493    errno = ENOSYS;
494    return -1;
495   break;
496 }
497
498 return 0;
499}
500
501static inline int _ioctl_stream_format_list (void) {
502 int format = 0;
503
504 format |= AFMT_S8;
505 format |= AFMT_U8;
506
507 format |= AFMT_S16_BE;
508 format |= AFMT_S16_LE;
509
510 format |= AFMT_U16_BE;
511 format |= AFMT_U16_LE;
512
513#ifdef AFMT_S32_BE
514 format |= AFMT_S32_BE;
515#endif
516#ifdef AFMT_S32_LE
517 format |= AFMT_S32_LE;
518#endif
519
520 format |= AFMT_A_LAW;
521 format |= AFMT_MU_LAW;
522
523#ifdef AFMT_VORBIS
524 format |= AFMT_VORBIS;
525#endif
526
527 return format;
528}
529
530// -------------------------------------
531// mixer ioctls:
532// -------------------------------------
533
534static int _ioctl_mixer (struct handle * handle, long unsigned int req, void * vp) {
535 mixer_info * info;
536 int channels;
537 struct roar_mixer_settings mixer;
538 int o_w    =  0;
539 int o_sid  = -1;
540 int * ip   = vp;
541#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
542 char * name = NULL;
543#endif
544
545#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
546 switch (req) {
547#if 0
548  case SNDCTL_MIX_DESCRIPTION: name = "SNDCTL_MIX_DESCRIPTION"; break;
549  case SNDCTL_MIX_ENUMINFO:    name = "SNDCTL_MIX_ENUMINFO";    break;
550  case SNDCTL_MIX_EXTINFO:     name = "SNDCTL_MIX_EXTINFO";     break;
551  case SNDCTL_MIX_NREXT:       name = "SNDCTL_MIX_NREXT";       break;
552  case SNDCTL_MIX_NRMIX:       name = "SNDCTL_MIX_NRMIX";       break;
553  case SNDCTL_MIX_READ:        name = "SNDCTL_MIX_READ";        break;
554  case SNDCTL_MIX_WRITE:       name = "SNDCTL_MIX_WRITE";       break;
555#endif
556//  case SOUND_MIXER_INFO:             name = "SOUND_MIXER_INFO";             break;
557  case SOUND_OLD_MIXER_INFO:         name = "SOUND_OLD_MIXER_INFO";         break;
558  case SOUND_MIXER_ACCESS:           name = "SOUND_MIXER_ACCESS";           break;
559  case SOUND_MIXER_AGC:              name = "SOUND_MIXER_AGC";              break;
560  case SOUND_MIXER_3DSE:             name = "SOUND_MIXER_3DSE";             break;
561  case SOUND_MIXER_GETLEVELS:        name = "SOUND_MIXER_GETLEVELS";        break;
562  case SOUND_MIXER_SETLEVELS:        name = "SOUND_MIXER_SETLEVELS";        break;
563  case SOUND_MIXER_PRIVATE1:         name = "SOUND_MIXER_PRIVATE1";         break;
564  case SOUND_MIXER_PRIVATE2:         name = "SOUND_MIXER_PRIVATE2";         break;
565  case SOUND_MIXER_PRIVATE3:         name = "SOUND_MIXER_PRIVATE3";         break;
566  case SOUND_MIXER_PRIVATE4:         name = "SOUND_MIXER_PRIVATE4";         break;
567  case SOUND_MIXER_PRIVATE5:         name = "SOUND_MIXER_PRIVATE5";         break;
568  case OSS_GETVERSION:               name = "OSS_GETVERSION";               break;
569//  case SOUND_MIXER_READ_CAPS:        name = "SOUND_MIXER_READ_CAPS";        break;
570  case SOUND_MIXER_READ_MUTE:        name = "SOUND_MIXER_READ_MUTE";        break;
571/*
572  case :     name = "";     break;
573  case :     name = "";     break;
574*/
575 }
576 if ( name != NULL ) {
577  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unspported mixer command %s", handle, req, ip, name);
578  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
579  errno = ENOSYS;
580  return -1;
581 }
582#endif
583
584 switch (req) {
585  case SOUND_MIXER_READ_VOLUME:    o_w = 0; o_sid = _mix_settings.sid.volume;   break;
586  case SOUND_MIXER_READ_LINE:      o_w = 0; o_sid = _mix_settings.sid.line;     break;
587  case SOUND_MIXER_READ_LINE1:     o_w = 0; o_sid = _mix_settings.sid.line1;    break;
588  case SOUND_MIXER_READ_LINE2:     o_w = 0; o_sid = _mix_settings.sid.line2;    break;
589  case SOUND_MIXER_READ_LINE3:     o_w = 0; o_sid = _mix_settings.sid.line3;    break;
590#if 0
591  case SOUND_MIXER_READ_DIGITAL1:  o_w = 0; o_sid = _mix_settings.sid.digital1; break;
592  case SOUND_MIXER_READ_DIGITAL2:  o_w = 0; o_sid = _mix_settings.sid.digital2; break;
593  case SOUND_MIXER_READ_DIGITAL3:  o_w = 0; o_sid = _mix_settings.sid.digital3; break;
594#endif
595  case SOUND_MIXER_WRITE_VOLUME:   o_w = 1; o_sid = _mix_settings.sid.volume;   break;
596  case SOUND_MIXER_WRITE_LINE:     o_w = 1; o_sid = _mix_settings.sid.line;     break;
597  case SOUND_MIXER_WRITE_LINE1:    o_w = 1; o_sid = _mix_settings.sid.line1;    break;
598  case SOUND_MIXER_WRITE_LINE2:    o_w = 1; o_sid = _mix_settings.sid.line2;    break;
599  case SOUND_MIXER_WRITE_LINE3:    o_w = 1; o_sid = _mix_settings.sid.line3;    break;
600#if 0
601  case SOUND_MIXER_WRITE_DIGITAL1: o_w = 1; o_sid = _mix_settings.sid.digital1; break;
602  case SOUND_MIXER_WRITE_DIGITAL2: o_w = 1; o_sid = _mix_settings.sid.digital2; break;
603  case SOUND_MIXER_WRITE_DIGITAL3: o_w = 1; o_sid = _mix_settings.sid.digital3; break;
604#endif
605  // we handle PCM seperatly as we want to be abled to abled to handle it on a stream (not mixer), too:
606  case SOUND_MIXER_READ_PCM:
607    o_w = 0;
608    if ( handle->type == HT_STREAM ) {
609     o_sid = roar_stream_get_id(&(handle->stream));
610    } else {
611     o_sid = _mix_settings.sid.pcm;
612    }
613   break;
614  case SOUND_MIXER_WRITE_PCM:
615    o_w = 1;
616    if ( handle->type == HT_STREAM ) {
617     o_sid = roar_stream_get_id(&(handle->stream));
618    } else {
619     o_sid = _mix_settings.sid.pcm;
620    }
621   break;
622 }
623 if ( o_sid != -1 ) {
624  // set/get volume
625  if ( o_w ) {
626   mixer.scale    = 65535;
627   mixer.mixer[0] = ( *ip       & 0xFF)*65535/OSS_VOLUME_SCALE;
628   mixer.mixer[1] = ((*ip >> 8) & 0xFF)*65535/OSS_VOLUME_SCALE;
629   if ( roar_set_vol(&(handle->session->con), o_sid, &mixer, 2) == -1 ) {
630    errno = EIO;
631    return -1;
632   }
633   return 0;
634  } else {
635   if ( roar_get_vol(&(handle->session->con), o_sid, &mixer, &channels) == -1 ) {
636    errno = EIO;
637    return -1;
638   }
639   *ip = ((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale) | (((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale)<<8);
640   return 0;
641  }
642 }
643
644 switch (req) {
645  case SOUND_MIXER_READ_STEREODEVS: /* FIXME: check the streams for channel config */
646  case SOUND_MIXER_READ_DEVMASK:
647    *ip = 0;
648
649    if ( _mix_settings.sid.volume != -1 )
650     *ip |= SOUND_MASK_VOLUME;
651    if ( _mix_settings.sid.pcm != -1 )
652     *ip |= SOUND_MASK_PCM;
653    if ( _mix_settings.sid.line != -1 )
654     *ip |= SOUND_MASK_LINE;
655    if ( _mix_settings.sid.line1 != -1 )
656     *ip |= SOUND_MASK_LINE1;
657    if ( _mix_settings.sid.line2 != -1 )
658     *ip |= SOUND_MASK_LINE2;
659    if ( _mix_settings.sid.line3 != -1 )
660     *ip |= SOUND_MASK_LINE3;
661    if ( _mix_settings.sid.digital1 != -1 )
662#if 0
663     *ip |= SOUND_MASK_DIGITAL1;
664    if ( _mix_settings.sid.digital2 != -1 )
665     *ip |= SOUND_MASK_DIGITAL2;
666    if ( _mix_settings.sid.digital3 != -1 )
667     *ip |= SOUND_MASK_DIGITAL3;
668#endif
669
670    return 0;
671   break;
672  case SOUND_MIXER_READ_RECMASK:
673  case SOUND_MIXER_READ_RECSRC:
674    *ip = SOUND_MASK_VOLUME; // we can currently only read from mixer
675    return 0;
676   break;
677  case SOUND_MIXER_WRITE_RECSRC:
678    if ( *ip == SOUND_MASK_VOLUME ) {
679     return  0;
680    } else {
681     errno = ENOTSUP;
682     return -1;
683    }
684   break;
685  case SOUND_MIXER_READ_CAPS:
686    *ip = 0;
687    return 0;
688   break;
689  case SOUND_MIXER_INFO:
690    info = vp;
691    memset(info, 0, sizeof(*info));
692    strcpy(info->id, "RoarAudio");
693    strcpy(info->name, "RoarAudio");
694    return 0;
695   break;
696 }
697
698 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unknown mixer CTL", handle, req, ip);
699// _os.ioctl(-1, req, ip);
700 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
701 errno = ENOSYS;
702 return -1;
703}
704
705// -------------------------------------
706// buffer size calculation:
707// -------------------------------------
708
709static size_t _get_stream_buffersize (struct handle * handle) {
710 if ( handle->stream_buffersize )
711  return handle->stream_buffersize;
712
713 return handle->stream_buffersize = handle->stream.info.rate     *
714                                    handle->stream.info.channels *
715                                    handle->stream.info.bits     / 800;
716}
717
718// -------------------------------------
719// emulated functions follow:
720// -------------------------------------
721
722int     open(const char *pathname, int flags, ...) {
723 int     ret;
724 mode_t  mode = 0;
725 va_list args;
726
727 _init();
728
729 ret = _open_file(pathname, flags);
730
731 switch (ret) {
732  case -2:       // continue as normal, use _op.open()
733   break;
734  case -1:       // pass error to caller
735    return -1;
736   break;
737  default:       // return successfully opened pointer to caller
738    return ret;
739   break;
740 }
741
742 if (flags & O_CREAT) {
743  va_start(args, flags);
744  mode = va_arg(args, mode_t);
745  va_end(args);
746 }
747
748 return _os.open(pathname, flags, mode);
749}
750
751int     close(int fd) {
752 struct pointer * pointer;
753 _init();
754
755 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
756  _close_pointer(pointer);
757  return 0;
758 }
759
760 return _os.close(fd);
761}
762
763ssize_t write(int fd, const void *buf, size_t count) {
764 struct pointer * pointer;
765 ssize_t ret;
766
767 _init();
768
769 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
770  if ( pointer->handle->type == HT_STREAM ) {
771   if ( pointer->handle->stream_opened == 0 ) {
772    if ( _open_stream(pointer->handle) == -1 ) {
773     errno = EIO;
774     return -1;
775    }
776   }
777   ret = roar_vio_write(&(pointer->handle->stream_vio), (char*)buf, count);
778   if ( ret > 0 )
779    pointer->handle->writec += ret;
780   return ret;
781  } else {
782   errno = EINVAL;
783   return -1;
784  }
785 }
786
787 return _os.write(fd, buf, count);
788}
789
790ssize_t read(int fd, void *buf, size_t count) {
791 struct pointer * pointer;
792 ssize_t ret;
793
794 _init();
795
796 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
797  if ( pointer->handle->type == HT_STREAM ) {
798   if ( pointer->handle->stream_opened == 0 ) {
799    if ( _open_stream(pointer->handle) == -1 ) {
800     errno = EIO;
801     return -1;
802    }
803   }
804   ret = roar_vio_read(&(pointer->handle->stream_vio), buf, count);
805   if ( ret > 0 )
806    pointer->handle->writec += ret;
807   return ret;
808  } else {
809   errno = EINVAL;
810   return -1;
811  }
812 }
813
814 return _os.read(fd, buf, count);
815}
816
817IOCTL() {
818 map_args;
819 struct pointer * pointer;
820 struct handle  * handle;
821 int * ip = NULL;
822 audio_buf_info * bi;
823 count_info     * ci;
824#ifdef va_argp
825 va_list args;
826#endif
827
828 _init();
829
830// ROAR_DBG("ioctl(__fd=%i, __request=%lu) = ?", __fd, (long unsigned int) __request);
831
832#ifdef va_argp
833 va_start (args, ioctl_lastarg);
834 argp = va_arg (args, void *);
835 va_end (args);
836#endif
837
838// ROAR_DBG("ioctl(__fd=%i, __request=%lu): argp=%p", __fd, (long unsigned int) __request, argp);
839
840 if ( (pointer = _get_pointer_by_fh(__fd)) != NULL ) {
841  ip = argp;
842//  ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
843  switch ((handle = pointer->handle)->type) {
844   case HT_STREAM:
845     switch (__request) {
846      case SNDCTL_DSP_RESET:
847      case SNDCTL_DSP_POST:
848      case SNDCTL_DSP_SETFRAGMENT: // any fragments should be ok for us...
849        return 0;
850       break;
851      case SNDCTL_DSP_SPEED:
852        handle->stream.info.rate = *ip;
853        return 0;
854       break;
855      case SNDCTL_DSP_CHANNELS:
856        handle->stream.info.channels = *ip;
857        return 0;
858       break;
859      case SNDCTL_DSP_STEREO:
860        handle->stream.info.channels = *ip ? 2 : 1;
861        return 0;
862       break;
863      case SNDCTL_DSP_GETBLKSIZE:
864        *ip = _get_stream_buffersize(handle);
865        return 0;
866       break;
867      case SNDCTL_DSP_SETFMT:
868        return _ioctl_stream_format(handle, *ip);
869       break;
870      case SNDCTL_DSP_GETFMTS:
871//        ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
872        *ip = _ioctl_stream_format_list();
873        return 0;
874       break;
875      case SNDCTL_DSP_GETOSPACE:
876      case SNDCTL_DSP_GETISPACE:
877        bi = argp;
878        memset(bi, 0, sizeof(*bi));
879        bi->bytes      = _get_stream_buffersize(handle);
880        bi->fragments  = 1;
881        bi->fragsize   = bi->bytes;
882        bi->fragstotal = 1;
883        return 0;
884       break;
885      case SNDCTL_DSP_GETOPTR:
886        ci = argp;
887        memset(ci, 0, sizeof(*ci));
888        ci->bytes  = handle->writec;
889        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
890        ci->ptr    = 0;
891        return 0;
892       break;
893      case SNDCTL_DSP_GETIPTR:
894        ci = argp;
895        memset(ci, 0, sizeof(*ci));
896        ci->bytes  = handle->readc;
897        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
898        ci->ptr    = 0;
899        return 0;
900       break;
901#ifdef SNDCTL_DSP_GETPLAYVOL
902      case SNDCTL_DSP_GETPLAYVOL:
903        return _ioctl_mixer(handle, SOUND_MIXER_READ_PCM, argp);
904       break;
905#endif
906#ifdef SNDCTL_DSP_SETPLAYVOL
907      case SNDCTL_DSP_SETPLAYVOL:
908        return _ioctl_mixer(handle, SOUND_MIXER_WRITE_PCM, argp);
909       break;
910#endif
911      default:
912        ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
913        errno = ENOSYS;
914        return -1;
915     }
916    break;
917   case HT_MIXER:
918     return _ioctl_mixer(handle, __request, argp);
919    break;
920   default:
921     ROAR_DBG("ioctl(__fd=%i, __request=%lu): unknown handle type: no ioctl()s supported", __fd, (long unsigned int) __request);
922     ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
923     errno = EINVAL;
924     return -1;
925    break;
926  }
927 }
928
929#ifdef IOCTL_IS_ALIAS
930 errno = ENOSYS;
931 return -1;
932#else
933 return _os.ioctl(__fd, __request, argp);
934#endif
935}
936
937#endif
938
939//ll
Note: See TracBrowser for help on using the repository browser.