source: roaraudio/libroaross/libroaross.c @ 3229:bbf20dac188c

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

added some really _bad_ debugging code

File size: 25.8 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    ROAR_DBG("_ioctl_stream_format(*): unsupported format");
497    errno = ENOSYS;
498    return -1;
499   break;
500 }
501
502 return 0;
503}
504
505static inline int _ioctl_stream_format_list (void) {
506 int format = 0;
507
508 format |= AFMT_S8;
509 format |= AFMT_U8;
510
511 format |= AFMT_S16_BE;
512 format |= AFMT_S16_LE;
513
514 format |= AFMT_U16_BE;
515 format |= AFMT_U16_LE;
516
517#ifdef AFMT_S32_BE
518 format |= AFMT_S32_BE;
519#endif
520#ifdef AFMT_S32_LE
521 format |= AFMT_S32_LE;
522#endif
523
524 format |= AFMT_A_LAW;
525 format |= AFMT_MU_LAW;
526
527#ifdef AFMT_VORBIS
528 format |= AFMT_VORBIS;
529#endif
530
531 return format;
532}
533
534// -------------------------------------
535// mixer ioctls:
536// -------------------------------------
537
538static int _ioctl_mixer (struct handle * handle, long unsigned int req, void * vp) {
539 mixer_info * info;
540 int channels;
541 struct roar_mixer_settings mixer;
542 int o_w    =  0;
543 int o_sid  = -1;
544 int * ip   = vp;
545#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
546 char * name = NULL;
547#endif
548
549#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
550 switch (req) {
551#if 0
552  case SNDCTL_MIX_DESCRIPTION: name = "SNDCTL_MIX_DESCRIPTION"; break;
553  case SNDCTL_MIX_ENUMINFO:    name = "SNDCTL_MIX_ENUMINFO";    break;
554  case SNDCTL_MIX_EXTINFO:     name = "SNDCTL_MIX_EXTINFO";     break;
555  case SNDCTL_MIX_NREXT:       name = "SNDCTL_MIX_NREXT";       break;
556  case SNDCTL_MIX_NRMIX:       name = "SNDCTL_MIX_NRMIX";       break;
557  case SNDCTL_MIX_READ:        name = "SNDCTL_MIX_READ";        break;
558  case SNDCTL_MIX_WRITE:       name = "SNDCTL_MIX_WRITE";       break;
559#endif
560//  case SOUND_MIXER_INFO:             name = "SOUND_MIXER_INFO";             break;
561  case SOUND_OLD_MIXER_INFO:         name = "SOUND_OLD_MIXER_INFO";         break;
562  case SOUND_MIXER_ACCESS:           name = "SOUND_MIXER_ACCESS";           break;
563  case SOUND_MIXER_AGC:              name = "SOUND_MIXER_AGC";              break;
564  case SOUND_MIXER_3DSE:             name = "SOUND_MIXER_3DSE";             break;
565  case SOUND_MIXER_GETLEVELS:        name = "SOUND_MIXER_GETLEVELS";        break;
566  case SOUND_MIXER_SETLEVELS:        name = "SOUND_MIXER_SETLEVELS";        break;
567  case SOUND_MIXER_PRIVATE1:         name = "SOUND_MIXER_PRIVATE1";         break;
568  case SOUND_MIXER_PRIVATE2:         name = "SOUND_MIXER_PRIVATE2";         break;
569  case SOUND_MIXER_PRIVATE3:         name = "SOUND_MIXER_PRIVATE3";         break;
570  case SOUND_MIXER_PRIVATE4:         name = "SOUND_MIXER_PRIVATE4";         break;
571  case SOUND_MIXER_PRIVATE5:         name = "SOUND_MIXER_PRIVATE5";         break;
572  case OSS_GETVERSION:               name = "OSS_GETVERSION";               break;
573//  case SOUND_MIXER_READ_CAPS:        name = "SOUND_MIXER_READ_CAPS";        break;
574  case SOUND_MIXER_READ_MUTE:        name = "SOUND_MIXER_READ_MUTE";        break;
575/*
576  case :     name = "";     break;
577  case :     name = "";     break;
578*/
579 }
580 if ( name != NULL ) {
581  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unspported mixer command %s", handle, req, ip, name);
582  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
583  errno = ENOSYS;
584  return -1;
585 }
586#endif
587
588 switch (req) {
589  case SOUND_MIXER_READ_VOLUME:    o_w = 0; o_sid = _mix_settings.sid.volume;   break;
590  case SOUND_MIXER_READ_LINE:      o_w = 0; o_sid = _mix_settings.sid.line;     break;
591  case SOUND_MIXER_READ_LINE1:     o_w = 0; o_sid = _mix_settings.sid.line1;    break;
592  case SOUND_MIXER_READ_LINE2:     o_w = 0; o_sid = _mix_settings.sid.line2;    break;
593  case SOUND_MIXER_READ_LINE3:     o_w = 0; o_sid = _mix_settings.sid.line3;    break;
594#if 0
595  case SOUND_MIXER_READ_DIGITAL1:  o_w = 0; o_sid = _mix_settings.sid.digital1; break;
596  case SOUND_MIXER_READ_DIGITAL2:  o_w = 0; o_sid = _mix_settings.sid.digital2; break;
597  case SOUND_MIXER_READ_DIGITAL3:  o_w = 0; o_sid = _mix_settings.sid.digital3; break;
598#endif
599  case SOUND_MIXER_WRITE_VOLUME:   o_w = 1; o_sid = _mix_settings.sid.volume;   break;
600  case SOUND_MIXER_WRITE_LINE:     o_w = 1; o_sid = _mix_settings.sid.line;     break;
601  case SOUND_MIXER_WRITE_LINE1:    o_w = 1; o_sid = _mix_settings.sid.line1;    break;
602  case SOUND_MIXER_WRITE_LINE2:    o_w = 1; o_sid = _mix_settings.sid.line2;    break;
603  case SOUND_MIXER_WRITE_LINE3:    o_w = 1; o_sid = _mix_settings.sid.line3;    break;
604#if 0
605  case SOUND_MIXER_WRITE_DIGITAL1: o_w = 1; o_sid = _mix_settings.sid.digital1; break;
606  case SOUND_MIXER_WRITE_DIGITAL2: o_w = 1; o_sid = _mix_settings.sid.digital2; break;
607  case SOUND_MIXER_WRITE_DIGITAL3: o_w = 1; o_sid = _mix_settings.sid.digital3; break;
608#endif
609  // we handle PCM seperatly as we want to be abled to abled to handle it on a stream (not mixer), too:
610  case SOUND_MIXER_READ_PCM:
611    o_w = 0;
612    if ( handle->type == HT_STREAM ) {
613     o_sid = roar_stream_get_id(&(handle->stream));
614    } else {
615     o_sid = _mix_settings.sid.pcm;
616    }
617   break;
618  case SOUND_MIXER_WRITE_PCM:
619    o_w = 1;
620    if ( handle->type == HT_STREAM ) {
621     o_sid = roar_stream_get_id(&(handle->stream));
622    } else {
623     o_sid = _mix_settings.sid.pcm;
624    }
625   break;
626 }
627 if ( o_sid != -1 ) {
628  // set/get volume
629  if ( o_w ) {
630   mixer.scale    = 65535;
631   mixer.mixer[0] = ( *ip       & 0xFF)*65535/OSS_VOLUME_SCALE;
632   mixer.mixer[1] = ((*ip >> 8) & 0xFF)*65535/OSS_VOLUME_SCALE;
633   if ( roar_set_vol(&(handle->session->con), o_sid, &mixer, 2) == -1 ) {
634    errno = EIO;
635    return -1;
636   }
637   return 0;
638  } else {
639   if ( roar_get_vol(&(handle->session->con), o_sid, &mixer, &channels) == -1 ) {
640    errno = EIO;
641    return -1;
642   }
643   *ip = ((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale) | (((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale)<<8);
644   return 0;
645  }
646 }
647
648 switch (req) {
649  case SOUND_MIXER_READ_STEREODEVS: /* FIXME: check the streams for channel config */
650  case SOUND_MIXER_READ_DEVMASK:
651    *ip = 0;
652
653    if ( _mix_settings.sid.volume != -1 )
654     *ip |= SOUND_MASK_VOLUME;
655    if ( _mix_settings.sid.pcm != -1 )
656     *ip |= SOUND_MASK_PCM;
657    if ( _mix_settings.sid.line != -1 )
658     *ip |= SOUND_MASK_LINE;
659    if ( _mix_settings.sid.line1 != -1 )
660     *ip |= SOUND_MASK_LINE1;
661    if ( _mix_settings.sid.line2 != -1 )
662     *ip |= SOUND_MASK_LINE2;
663    if ( _mix_settings.sid.line3 != -1 )
664     *ip |= SOUND_MASK_LINE3;
665    if ( _mix_settings.sid.digital1 != -1 )
666#if 0
667     *ip |= SOUND_MASK_DIGITAL1;
668    if ( _mix_settings.sid.digital2 != -1 )
669     *ip |= SOUND_MASK_DIGITAL2;
670    if ( _mix_settings.sid.digital3 != -1 )
671     *ip |= SOUND_MASK_DIGITAL3;
672#endif
673
674    return 0;
675   break;
676  case SOUND_MIXER_READ_RECMASK:
677  case SOUND_MIXER_READ_RECSRC:
678    *ip = SOUND_MASK_VOLUME; // we can currently only read from mixer
679    return 0;
680   break;
681  case SOUND_MIXER_WRITE_RECSRC:
682    if ( *ip == SOUND_MASK_VOLUME ) {
683     return  0;
684    } else {
685     errno = ENOTSUP;
686     return -1;
687    }
688   break;
689  case SOUND_MIXER_READ_CAPS:
690    *ip = 0;
691    return 0;
692   break;
693  case SOUND_MIXER_INFO:
694    info = vp;
695    memset(info, 0, sizeof(*info));
696    strcpy(info->id, "RoarAudio");
697    strcpy(info->name, "RoarAudio");
698    return 0;
699   break;
700 }
701
702 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unknown mixer CTL", handle, req, ip);
703// _os.ioctl(-1, req, ip);
704 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
705 errno = ENOSYS;
706 return -1;
707}
708
709// -------------------------------------
710// buffer size calculation:
711// -------------------------------------
712
713static size_t _get_stream_buffersize (struct handle * handle) {
714 if ( handle->stream_buffersize )
715  return handle->stream_buffersize;
716
717 return handle->stream_buffersize = handle->stream.info.rate     *
718                                    handle->stream.info.channels *
719                                    handle->stream.info.bits     / 800;
720}
721
722// -------------------------------------
723// emulated functions follow:
724// -------------------------------------
725
726int     open(const char *pathname, int flags, ...) {
727 int     ret;
728 mode_t  mode = 0;
729 va_list args;
730
731 _init();
732
733 ret = _open_file(pathname, flags);
734
735 switch (ret) {
736  case -2:       // continue as normal, use _op.open()
737   break;
738  case -1:       // pass error to caller
739    return -1;
740   break;
741  default:       // return successfully opened pointer to caller
742    return ret;
743   break;
744 }
745
746 if (flags & O_CREAT) {
747  va_start(args, flags);
748  mode = va_arg(args, mode_t);
749  va_end(args);
750 }
751
752 return _os.open(pathname, flags, mode);
753}
754
755int     close(int fd) {
756 struct pointer * pointer;
757 _init();
758
759 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
760  _close_pointer(pointer);
761  return 0;
762 }
763
764 return _os.close(fd);
765}
766
767ssize_t write(int fd, const void *buf, size_t count) {
768 struct pointer * pointer;
769 ssize_t ret;
770
771 _init();
772
773 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
774  if ( pointer->handle->type == HT_STREAM ) {
775   if ( pointer->handle->stream_opened == 0 ) {
776    if ( _open_stream(pointer->handle) == -1 ) {
777     errno = EIO;
778     return -1;
779    }
780   }
781   ret = roar_vio_write(&(pointer->handle->stream_vio), (char*)buf, count);
782   if ( ret > 0 )
783    pointer->handle->writec += ret;
784   return ret;
785  } else {
786   errno = EINVAL;
787   return -1;
788  }
789 }
790
791 return _os.write(fd, buf, count);
792}
793
794ssize_t read(int fd, void *buf, size_t count) {
795 struct pointer * pointer;
796 ssize_t ret;
797
798 _init();
799
800 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
801  if ( pointer->handle->type == HT_STREAM ) {
802   if ( pointer->handle->stream_opened == 0 ) {
803    if ( _open_stream(pointer->handle) == -1 ) {
804     errno = EIO;
805     return -1;
806    }
807   }
808   ret = roar_vio_read(&(pointer->handle->stream_vio), buf, count);
809   if ( ret > 0 )
810    pointer->handle->writec += ret;
811   return ret;
812  } else {
813   errno = EINVAL;
814   return -1;
815  }
816 }
817
818 return _os.read(fd, buf, count);
819}
820
821IOCTL() {
822 map_args;
823 struct pointer * pointer;
824 struct handle  * handle;
825 int * ip = NULL;
826 audio_buf_info * bi;
827 count_info     * ci;
828#ifdef __FIXME__
829 char * nosys_reqname = NULL;
830#endif
831#ifdef va_argp
832 va_list args;
833#endif
834
835 _init();
836
837// ROAR_DBG("ioctl(__fd=%i, __request=%lu) = ?", __fd, (long unsigned int) __request);
838
839#ifdef va_argp
840 va_start (args, ioctl_lastarg);
841 argp = va_arg (args, void *);
842 va_end (args);
843#endif
844
845// ROAR_DBG("ioctl(__fd=%i, __request=%lu): argp=%p", __fd, (long unsigned int) __request, argp);
846
847 if ( (pointer = _get_pointer_by_fh(__fd)) != NULL ) {
848  ip = argp;
849//  ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
850#ifdef __FIXME__
851  switch ((handle = pointer->handle)->type) {
852   case SOUND_PCM_READ_RATE: nosys_reqname = "SOUND_PCM_READ_RATE"; break;
853   case SOUND_PCM_READ_CHANNELS: nosys_reqname = "SOUND_PCM_READ_CHANNELS"; break;
854   case SOUND_PCM_READ_BITS: nosys_reqname = "SOUND_PCM_READ_BITS"; break;
855   case SOUND_PCM_READ_FILTER: nosys_reqname = "SOUND_PCM_READ_FILTER"; break;
856   case SNDCTL_COPR_RESET: nosys_reqname = "SNDCTL_COPR_RESET"; break;
857   case SNDCTL_COPR_LOAD: nosys_reqname = "SNDCTL_COPR_LOAD"; break;
858   case SNDCTL_COPR_HALT: nosys_reqname = "SNDCTL_COPR_HALT"; break;
859   case SNDCTL_COPR_RDATA: nosys_reqname = "SNDCTL_COPR_RDATA"; break;
860   case SNDCTL_COPR_RCODE: nosys_reqname = "SNDCTL_COPR_RCODE"; break;
861   case SNDCTL_COPR_WDATA: nosys_reqname = "SNDCTL_COPR_WDATA"; break;
862   case SNDCTL_COPR_WCODE: nosys_reqname = "SNDCTL_COPR_WCODE"; break;
863   case SNDCTL_COPR_RUN: nosys_reqname = "SNDCTL_COPR_RUN"; break;
864   case SNDCTL_COPR_SENDMSG: nosys_reqname = "SNDCTL_COPR_SENDMSG"; break;
865   case SNDCTL_COPR_RCVMSG: nosys_reqname = "SNDCTL_COPR_RCVMSG"; break;
866   case SNDCTL_DSP_SPEED: nosys_reqname = "SNDCTL_DSP_SPEED"; break;
867/*
868   case : nosys_reqname = ""; break;
869   case : nosys_reqname = ""; break;
870   case : nosys_reqname = ""; break;
871*/
872  }
873#endif
874  switch ((handle = pointer->handle)->type) {
875   case HT_STREAM:
876     switch (__request) {
877      case SNDCTL_DSP_RESET:
878      case SNDCTL_DSP_POST:
879      case SNDCTL_DSP_SETFRAGMENT: // any fragments should be ok for us...
880        return 0;
881       break;
882      case SNDCTL_DSP_SPEED:
883        handle->stream.info.rate = *ip;
884        ROAR_DBG("ioctl(__fd=%i, __request=%lu): rate=%i", __fd, (long unsigned int) __request, *ip);
885        return 0;
886       break;
887      case SNDCTL_DSP_CHANNELS:
888        handle->stream.info.channels = *ip;
889        ROAR_DBG("ioctl(__fd=%i, __request=%lu): channels=%i", __fd, (long unsigned int) __request, *ip);
890        return 0;
891       break;
892      case SNDCTL_DSP_STEREO:
893        handle->stream.info.channels = *ip ? 2 : 1;
894        return 0;
895       break;
896      case SNDCTL_DSP_GETBLKSIZE:
897        *ip = _get_stream_buffersize(handle);
898        return 0;
899       break;
900      case SNDCTL_DSP_SETFMT:
901        ROAR_DBG("ioctl(__fd=%i, __request=%lu): fmt=0x%x", __fd, (long unsigned int) __request, *ip);
902        return _ioctl_stream_format(handle, *ip);
903       break;
904      case SNDCTL_DSP_GETFMTS:
905//        ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
906        *ip = _ioctl_stream_format_list();
907        return 0;
908       break;
909      case SNDCTL_DSP_GETOSPACE:
910      case SNDCTL_DSP_GETISPACE:
911        bi = argp;
912        memset(bi, 0, sizeof(*bi));
913        bi->bytes      = _get_stream_buffersize(handle);
914        bi->fragments  = 1;
915        bi->fragsize   = bi->bytes;
916        bi->fragstotal = 1;
917        return 0;
918       break;
919      case SNDCTL_DSP_GETOPTR:
920        ci = argp;
921        memset(ci, 0, sizeof(*ci));
922        ci->bytes  = handle->writec;
923        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
924        ci->ptr    = 0;
925        return 0;
926       break;
927      case SNDCTL_DSP_GETIPTR:
928        ci = argp;
929        memset(ci, 0, sizeof(*ci));
930        ci->bytes  = handle->readc;
931        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
932        ci->ptr    = 0;
933        return 0;
934       break;
935#ifdef SNDCTL_DSP_GETPLAYVOL
936      case SNDCTL_DSP_GETPLAYVOL:
937        return _ioctl_mixer(handle, SOUND_MIXER_READ_PCM, argp);
938       break;
939#endif
940#ifdef SNDCTL_DSP_SETPLAYVOL
941      case SNDCTL_DSP_SETPLAYVOL:
942        return _ioctl_mixer(handle, SOUND_MIXER_WRITE_PCM, argp);
943       break;
944#endif
945      default:
946#ifdef __FIXME__
947        ROAR_DBG("ioctl(__fd=%i, __request=%lu (%s)) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request, nosys_reqname);
948#else
949        ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
950#endif
951        errno = ENOSYS;
952        return -1;
953     }
954    break;
955   case HT_MIXER:
956     return _ioctl_mixer(handle, __request, argp);
957    break;
958   default:
959     ROAR_DBG("ioctl(__fd=%i, __request=%lu): unknown handle type: no ioctl()s supported", __fd, (long unsigned int) __request);
960     ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
961     errno = EINVAL;
962     return -1;
963    break;
964  }
965 }
966
967#ifdef IOCTL_IS_ALIAS
968 errno = ENOSYS;
969 return -1;
970#else
971 return _os.ioctl(__fd, __request, argp);
972#endif
973}
974
975#endif
976
977//ll
Note: See TracBrowser for help on using the repository browser.