source: roaraudio/libroaross/libroaross.c @ 3244:cb520c8f0212

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

support for MIDI

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