source: roaraudio/libroaross/libroaross.c @ 3171:d99199cb5daf

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

avoide parsing probelms of older gcc versions

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/mixer", HT_MIXER},
344  {NULL, HT_NONE},
345 };
346 int i;
347
348 for (i = 0; p[i].prefix != NULL; i++) {
349  if ( !strcmp(pathname, p[i].prefix) ) {
350   ptr = &(p[i]);
351  }
352 }
353
354 if ( ptr == NULL )
355  return -2;
356
357 if ( (session = _open_session(NULL, NULL)) == NULL ) {
358  return -1;
359 }
360
361 if ( (handle = _open_handle(session)) == NULL ) {
362  _close_session(session);
363  return -1;
364 }
365
366 handle->type = ptr->type;
367
368 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
369  case O_RDONLY:
370    handle->stream_dir = ROAR_DIR_MONITOR;
371   break;
372  case O_WRONLY:
373    handle->stream_dir = ROAR_DIR_PLAY;
374   break;
375  case O_RDWR:
376    handle->stream_dir = ROAR_DIR_BIDIR;
377   break;
378 }
379
380 if ( (pointer = _open_pointer(handle)) == NULL ) {
381  _close_handle(handle);
382  return -1;
383 }
384
385 return pointer->fh;
386}
387
388// -------------------------------------
389// open function for streams:
390// -------------------------------------
391
392static int _open_stream (struct handle * handle) {
393  // FIXME: this should be re-written much more cleanly:
394
395 if ( handle == NULL )
396  return -1;
397
398 if ( roar_vio_simple_new_stream_obj(&(handle->stream_vio),
399                                     &(handle->session->con), &(handle->stream),
400                                     handle->stream.info.rate,
401                                     handle->stream.info.channels,
402                                     handle->stream.info.bits,
403                                     handle->stream.info.codec,
404                                     handle->stream_dir
405                                    ) == -1 )
406  return -1;
407
408 handle->stream_opened++;
409
410 _mix_settings.sid.pcm = roar_stream_get_id(&(handle->stream));
411
412 return 0;
413}
414
415// -------------------------------------
416// function to parse format:
417// -------------------------------------
418
419static int _ioctl_stream_format (struct handle * handle, int format) {
420 struct roar_audio_info * info = &(handle->stream.info);
421
422 switch (format) {
423  case AFMT_S8:
424    info->bits  = 8;
425    info->codec = ROAR_CODEC_PCM_S_LE;
426   break;
427  case AFMT_U8:
428    info->bits  = 8;
429    info->codec = ROAR_CODEC_PCM_U_LE;
430   break;
431  case AFMT_S16_BE:
432    info->bits  = 16;
433    info->codec = ROAR_CODEC_PCM_S_BE;
434   break;
435  case AFMT_S16_LE:
436    info->bits  = 16;
437    info->codec = ROAR_CODEC_PCM_S_LE;
438   break;
439  case AFMT_U16_BE:
440    info->bits  = 16;
441    info->codec = ROAR_CODEC_PCM_U_BE;
442   break;
443  case AFMT_U16_LE:
444    info->bits  = 16;
445    info->codec = ROAR_CODEC_PCM_U_LE;
446   break;
447#ifdef AFMT_S32_BE
448  case AFMT_S32_BE:
449    info->bits  = 32;
450    info->codec = ROAR_CODEC_PCM_S_BE;
451   break;
452#endif
453#ifdef AFMT_S32_LE
454  case AFMT_S32_LE:
455    info->bits  = 32;
456    info->codec = ROAR_CODEC_PCM_S_LE;
457   break;
458#endif
459  case AFMT_A_LAW:
460    info->bits  = 8;
461    info->codec = ROAR_CODEC_ALAW;
462   break;
463  case AFMT_MU_LAW:
464    info->bits  = 8;
465    info->codec = ROAR_CODEC_MULAW;
466   break;
467#ifdef AFMT_VORBIS
468  case AFMT_VORBIS:
469    info->codec = ROAR_CODEC_OGG_VORBIS;
470   break;
471#endif
472  default:
473    errno = ENOSYS;
474    return -1;
475   break;
476 }
477
478 return 0;
479}
480
481static inline int _ioctl_stream_format_list (void) {
482 int format = 0;
483
484 format |= AFMT_S8;
485 format |= AFMT_U8;
486
487 format |= AFMT_S16_BE;
488 format |= AFMT_S16_LE;
489
490 format |= AFMT_U16_BE;
491 format |= AFMT_U16_LE;
492
493#ifdef AFMT_S32_BE
494 format |= AFMT_S32_BE;
495#endif
496#ifdef AFMT_S32_LE
497 format |= AFMT_S32_LE;
498#endif
499
500 format |= AFMT_A_LAW;
501 format |= AFMT_MU_LAW;
502
503#ifdef AFMT_VORBIS
504 format |= AFMT_VORBIS;
505#endif
506
507 return format;
508}
509
510// -------------------------------------
511// mixer ioctls:
512// -------------------------------------
513
514static int _ioctl_mixer (struct handle * handle, long unsigned int req, void * vp) {
515 mixer_info * info;
516 int channels;
517 struct roar_mixer_settings mixer;
518 int o_w    =  0;
519 int o_sid  = -1;
520 int * ip   = vp;
521#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
522 char * name = NULL;
523#endif
524
525#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
526 switch (req) {
527#if 0
528  case SNDCTL_MIX_DESCRIPTION: name = "SNDCTL_MIX_DESCRIPTION"; break;
529  case SNDCTL_MIX_ENUMINFO:    name = "SNDCTL_MIX_ENUMINFO";    break;
530  case SNDCTL_MIX_EXTINFO:     name = "SNDCTL_MIX_EXTINFO";     break;
531  case SNDCTL_MIX_NREXT:       name = "SNDCTL_MIX_NREXT";       break;
532  case SNDCTL_MIX_NRMIX:       name = "SNDCTL_MIX_NRMIX";       break;
533  case SNDCTL_MIX_READ:        name = "SNDCTL_MIX_READ";        break;
534  case SNDCTL_MIX_WRITE:       name = "SNDCTL_MIX_WRITE";       break;
535#endif
536//  case SOUND_MIXER_INFO:             name = "SOUND_MIXER_INFO";             break;
537  case SOUND_OLD_MIXER_INFO:         name = "SOUND_OLD_MIXER_INFO";         break;
538  case SOUND_MIXER_ACCESS:           name = "SOUND_MIXER_ACCESS";           break;
539  case SOUND_MIXER_AGC:              name = "SOUND_MIXER_AGC";              break;
540  case SOUND_MIXER_3DSE:             name = "SOUND_MIXER_3DSE";             break;
541  case SOUND_MIXER_GETLEVELS:        name = "SOUND_MIXER_GETLEVELS";        break;
542  case SOUND_MIXER_SETLEVELS:        name = "SOUND_MIXER_SETLEVELS";        break;
543  case SOUND_MIXER_PRIVATE1:         name = "SOUND_MIXER_PRIVATE1";         break;
544  case SOUND_MIXER_PRIVATE2:         name = "SOUND_MIXER_PRIVATE2";         break;
545  case SOUND_MIXER_PRIVATE3:         name = "SOUND_MIXER_PRIVATE3";         break;
546  case SOUND_MIXER_PRIVATE4:         name = "SOUND_MIXER_PRIVATE4";         break;
547  case SOUND_MIXER_PRIVATE5:         name = "SOUND_MIXER_PRIVATE5";         break;
548  case OSS_GETVERSION:               name = "OSS_GETVERSION";               break;
549//  case SOUND_MIXER_READ_CAPS:        name = "SOUND_MIXER_READ_CAPS";        break;
550  case SOUND_MIXER_READ_MUTE:        name = "SOUND_MIXER_READ_MUTE";        break;
551/*
552  case :     name = "";     break;
553  case :     name = "";     break;
554*/
555 }
556 if ( name != NULL ) {
557  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unspported mixer command %s", handle, req, ip, name);
558  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
559  errno = ENOSYS;
560  return -1;
561 }
562#endif
563
564 switch (req) {
565  case SOUND_MIXER_READ_VOLUME:    o_w = 0; o_sid = _mix_settings.sid.volume;   break;
566  case SOUND_MIXER_READ_PCM:       o_w = 0; o_sid = _mix_settings.sid.pcm;      break;
567  case SOUND_MIXER_READ_LINE:      o_w = 0; o_sid = _mix_settings.sid.line;     break;
568  case SOUND_MIXER_READ_LINE1:     o_w = 0; o_sid = _mix_settings.sid.line1;    break;
569  case SOUND_MIXER_READ_LINE2:     o_w = 0; o_sid = _mix_settings.sid.line2;    break;
570  case SOUND_MIXER_READ_LINE3:     o_w = 0; o_sid = _mix_settings.sid.line3;    break;
571#if 0
572  case SOUND_MIXER_READ_DIGITAL1:  o_w = 0; o_sid = _mix_settings.sid.digital1; break;
573  case SOUND_MIXER_READ_DIGITAL2:  o_w = 0; o_sid = _mix_settings.sid.digital2; break;
574  case SOUND_MIXER_READ_DIGITAL3:  o_w = 0; o_sid = _mix_settings.sid.digital3; break;
575#endif
576  case SOUND_MIXER_WRITE_VOLUME:   o_w = 1; o_sid = _mix_settings.sid.volume;   break;
577  case SOUND_MIXER_WRITE_PCM:      o_w = 1; o_sid = _mix_settings.sid.pcm;      break;
578  case SOUND_MIXER_WRITE_LINE:     o_w = 1; o_sid = _mix_settings.sid.line;     break;
579  case SOUND_MIXER_WRITE_LINE1:    o_w = 1; o_sid = _mix_settings.sid.line1;    break;
580  case SOUND_MIXER_WRITE_LINE2:    o_w = 1; o_sid = _mix_settings.sid.line2;    break;
581  case SOUND_MIXER_WRITE_LINE3:    o_w = 1; o_sid = _mix_settings.sid.line3;    break;
582#if 0
583  case SOUND_MIXER_WRITE_DIGITAL1: o_w = 1; o_sid = _mix_settings.sid.digital1; break;
584  case SOUND_MIXER_WRITE_DIGITAL2: o_w = 1; o_sid = _mix_settings.sid.digital2; break;
585  case SOUND_MIXER_WRITE_DIGITAL3: o_w = 1; o_sid = _mix_settings.sid.digital3; break;
586#endif
587 }
588 if ( o_sid != -1 ) {
589  // set/get volume
590  if ( o_w ) {
591   mixer.scale    = 65535;
592   mixer.mixer[0] = ( *ip       & 0xFF)*65535/OSS_VOLUME_SCALE;
593   mixer.mixer[1] = ((*ip >> 8) & 0xFF)*65535/OSS_VOLUME_SCALE;
594   if ( roar_set_vol(&(handle->session->con), o_sid, &mixer, 2) == -1 ) {
595    errno = EIO;
596    return -1;
597   }
598   return 0;
599  } else {
600   if ( roar_get_vol(&(handle->session->con), o_sid, &mixer, &channels) == -1 ) {
601    errno = EIO;
602    return -1;
603   }
604   *ip = ((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale) | (((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale)<<8);
605   return 0;
606  }
607 }
608
609 switch (req) {
610  case SOUND_MIXER_READ_STEREODEVS: /* FIXME: check the streams for channel config */
611  case SOUND_MIXER_READ_DEVMASK:
612    *ip = 0;
613
614    if ( _mix_settings.sid.volume != -1 )
615     *ip |= SOUND_MASK_VOLUME;
616    if ( _mix_settings.sid.pcm != -1 )
617     *ip |= SOUND_MASK_PCM;
618    if ( _mix_settings.sid.line != -1 )
619     *ip |= SOUND_MASK_LINE;
620    if ( _mix_settings.sid.line1 != -1 )
621     *ip |= SOUND_MASK_LINE1;
622    if ( _mix_settings.sid.line2 != -1 )
623     *ip |= SOUND_MASK_LINE2;
624    if ( _mix_settings.sid.line3 != -1 )
625     *ip |= SOUND_MASK_LINE3;
626    if ( _mix_settings.sid.digital1 != -1 )
627#if 0
628     *ip |= SOUND_MASK_DIGITAL1;
629    if ( _mix_settings.sid.digital2 != -1 )
630     *ip |= SOUND_MASK_DIGITAL2;
631    if ( _mix_settings.sid.digital3 != -1 )
632     *ip |= SOUND_MASK_DIGITAL3;
633#endif
634
635    return 0;
636   break;
637  case SOUND_MIXER_READ_RECMASK:
638  case SOUND_MIXER_READ_RECSRC:
639    *ip = SOUND_MASK_VOLUME; // we can currently only read from mixer
640    return 0;
641   break;
642  case SOUND_MIXER_WRITE_RECSRC:
643    if ( *ip == SOUND_MASK_VOLUME ) {
644     return  0;
645    } else {
646     errno = ENOTSUP;
647     return -1;
648    }
649   break;
650  case SOUND_MIXER_READ_CAPS:
651    *ip = 0;
652    return 0;
653   break;
654  case SOUND_MIXER_INFO:
655    info = vp;
656    memset(info, 0, sizeof(*info));
657    strcpy(info->id, "RoarAudio");
658    strcpy(info->name, "RoarAudio");
659    return 0;
660   break;
661 }
662
663 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unknown mixer CTL", handle, req, ip);
664// _os.ioctl(-1, req, ip);
665 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
666 errno = ENOSYS;
667 return -1;
668}
669
670// -------------------------------------
671// emulated functions follow:
672// -------------------------------------
673
674int     open(const char *pathname, int flags, ...) {
675 int     ret;
676 mode_t  mode = 0;
677 va_list args;
678
679 _init();
680
681 ret = _open_file(pathname, flags);
682
683 switch (ret) {
684  case -2:       // continue as normal, use _op.open()
685   break;
686  case -1:       // pass error to caller
687    return -1;
688   break;
689  default:       // return successfully opened pointer to caller
690    return ret;
691   break;
692 }
693
694 if (flags & O_CREAT) {
695  va_start(args, flags);
696  mode = va_arg(args, mode_t);
697  va_end(args);
698 }
699
700 return _os.open(pathname, flags, mode);
701}
702
703int     close(int fd) {
704 struct pointer * pointer;
705 _init();
706
707 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
708  _close_pointer(pointer);
709  return 0;
710 }
711
712 return _os.close(fd);
713}
714
715ssize_t write(int fd, const void *buf, size_t count) {
716 struct pointer * pointer;
717
718 _init();
719
720 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
721  if ( pointer->handle->type == HT_STREAM ) {
722   if ( pointer->handle->stream_opened == 0 ) {
723    if ( _open_stream(pointer->handle) == -1 ) {
724     errno = EIO;
725     return -1;
726    }
727   }
728   return roar_vio_write(&(pointer->handle->stream_vio), (char*)buf, count);
729  } else {
730   errno = EINVAL;
731   return -1;
732  }
733 }
734
735 return _os.write(fd, buf, count);
736}
737
738ssize_t read(int fd, void *buf, size_t count) {
739 struct pointer * pointer;
740
741 _init();
742
743 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
744  if ( pointer->handle->type == HT_STREAM ) {
745   if ( pointer->handle->stream_opened == 0 ) {
746    if ( _open_stream(pointer->handle) == -1 ) {
747     errno = EIO;
748     return -1;
749    }
750   }
751   return roar_vio_read(&(pointer->handle->stream_vio), buf, count);
752  } else {
753   errno = EINVAL;
754   return -1;
755  }
756 }
757
758 return _os.read(fd, buf, count);
759}
760
761IOCTL() {
762 map_args;
763 struct pointer * pointer;
764 struct handle  * handle;
765 int * ip = NULL;
766#ifdef va_argp
767 va_list args;
768#endif
769
770 _init();
771
772// ROAR_DBG("ioctl(__fd=%i, __request=%lu) = ?", __fd, (long unsigned int) __request);
773
774#ifdef va_argp
775 va_start (args, __request);
776 argp = va_arg (args, void *);
777 va_end (args);
778#endif
779
780// ROAR_DBG("ioctl(__fd=%i, __request=%lu): argp=%p", __fd, (long unsigned int) __request, argp);
781
782 if ( (pointer = _get_pointer_by_fh(__fd)) != NULL ) {
783  ip = argp;
784//  ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
785  switch ((handle = pointer->handle)->type) {
786   case HT_STREAM:
787     switch (__request) {
788      case SNDCTL_DSP_RESET:
789      case SNDCTL_DSP_POST:
790      case SNDCTL_DSP_SETFRAGMENT: // any fragments should be ok for us...
791       break;
792      case SNDCTL_DSP_SPEED:
793        handle->stream.info.rate = *ip;
794        return 0;
795       break;
796      case SNDCTL_DSP_CHANNELS:
797        handle->stream.info.channels = *ip;
798        return 0;
799       break;
800      case SNDCTL_DSP_STEREO:
801        handle->stream.info.channels = *ip ? 2 : 1;
802        return 0;
803       break;
804      case SNDCTL_DSP_GETBLKSIZE:
805         *ip = handle->stream.info.rate * handle->stream.info.channels * handle->stream.info.bits / 800;
806        return 0;
807       break;
808      case SNDCTL_DSP_SETFMT:
809        return _ioctl_stream_format(handle, *ip);
810       break;
811      case SNDCTL_DSP_GETFMTS:
812//        ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
813        *ip = _ioctl_stream_format_list();
814        return 0;
815       break;
816      default:
817        ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
818        errno = ENOSYS;
819        return -1;
820     }
821    break;
822   case HT_MIXER:
823     return _ioctl_mixer(handle, __request, argp);
824    break;
825   default:
826     ROAR_DBG("ioctl(__fd=%i, __request=%lu): unknown handle type: no ioctl()s supported", __fd, (long unsigned int) __request);
827     ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
828     errno = EINVAL;
829     return -1;
830    break;
831  }
832 }
833
834#ifdef IOCTL_IS_ALIAS
835 errno = ENOSYS;
836 return -1;
837#else
838 return _os.ioctl(__fd, __request, argp);
839#endif
840}
841
842#endif
843
844//ll
Note: See TracBrowser for help on using the repository browser.