source: roaraudio/libroaross/libroaross.c @ 3246:1d9346abf116

Last change on this file since 3246:1d9346abf116 was 3246:1d9346abf116, checked in by phi, 14 years ago

start of DMX support

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