source: roaraudio/libroaross/libroaross.c @ 3173:e002d7cdffdc

Last change on this file since 3173:e002d7cdffdc was 3173:e002d7cdffdc, checked in by phi, 12 years ago

added /dev/audio to list of audio devices

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