source: roaraudio/libroaross/libroaross.c @ 3180:6f606ed52506

Last change on this file since 3180:6f606ed52506 was 3180:6f606ed52506, checked in by phi, 14 years ago

get it to build (not to work) on cygwin

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