source: roaraudio/libroaross/libroaross.c @ 3247:76ee0ad9cdc4

Last change on this file since 3247:76ee0ad9cdc4 was 3247:76ee0ad9cdc4, checked in by phi, 14 years ago

more DMX devices

File size: 27.9 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  {"/dev/misc/dmx",      HT_DMX},
379  {"/dev/dmxin",         HT_DMX},
380  {"/dev/misc/dmxin",    HT_DMX},
381#ifdef ROAR_DEFAULT_OSS_DEV
382  {ROAR_DEFAULT_OSS_DEV, HT_WAVEFORM},
383#endif
384  {NULL, HT_NONE},
385 };
386 int i;
387
388 for (i = 0; p[i].prefix != NULL; i++) {
389  if ( !strcmp(pathname, p[i].prefix) ) {
390   ptr = &(p[i]);
391  }
392 }
393
394 if ( ptr == NULL )
395  return -2;
396
397 if ( (session = _open_session(NULL, NULL)) == NULL ) {
398  return -1;
399 }
400
401 if ( (handle = _open_handle(session)) == NULL ) {
402  _close_session(session);
403  return -1;
404 }
405
406 handle->type       = ptr->type;
407 handle->stream_dir = -1;
408
409 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
410  case O_RDONLY:
411    switch (ptr->type) {
412     case HT_WAVEFORM:
413       handle->stream_dir = ROAR_DIR_MONITOR;
414      break;
415     case HT_MIDI:
416       handle->stream_dir = ROAR_DIR_MIDI_OUT;
417      break;
418     case HT_DMX:
419       handle->stream_dir = ROAR_DIR_LIGHT_OUT;
420      break;
421    }
422   break;
423  case O_WRONLY:
424    switch (ptr->type) {
425     case HT_WAVEFORM:
426       handle->stream_dir = ROAR_DIR_PLAY;
427      break;
428     case HT_MIDI:
429       handle->stream_dir = ROAR_DIR_MIDI_IN;
430      break;
431     case HT_DMX:
432       handle->stream_dir = ROAR_DIR_LIGHT_IN;
433      break;
434    }
435   break;
436  case O_RDWR:
437    switch (ptr->type) {
438     case HT_WAVEFORM:
439       handle->stream_dir = ROAR_DIR_BIDIR;
440      break;
441    }
442   break;
443 }
444
445 switch (handle->type) {
446  case HT_WAVEFORM:
447    handle->type = HT_STREAM;
448   break;
449  case HT_MIDI:
450    handle->type = HT_STREAM;
451    handle->stream.info.rate     = 0;
452    handle->stream.info.bits     = ROAR_MIDI_BITS;
453    handle->stream.info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
454    handle->stream.info.codec    = ROAR_CODEC_MIDI;
455   break;
456 }
457
458 if ( (pointer = _open_pointer(handle)) == NULL ) {
459  _close_handle(handle);
460  return -1;
461 }
462
463 return pointer->fh;
464}
465
466// -------------------------------------
467// open function for streams:
468// -------------------------------------
469
470static int _open_stream (struct handle * handle) {
471  // FIXME: this should be re-written much more cleanly:
472
473 if ( handle == NULL )
474  return -1;
475
476 if ( roar_vio_simple_new_stream_obj(&(handle->stream_vio),
477                                     &(handle->session->con), &(handle->stream),
478                                     handle->stream.info.rate,
479                                     handle->stream.info.channels,
480                                     handle->stream.info.bits,
481                                     handle->stream.info.codec,
482                                     handle->stream_dir
483                                    ) == -1 )
484  return -1;
485
486 handle->stream_opened++;
487
488 _mix_settings.sid.pcm = roar_stream_get_id(&(handle->stream));
489
490 return 0;
491}
492
493// -------------------------------------
494// function to parse format:
495// -------------------------------------
496
497static int _ioctl_stream_format (struct handle * handle, int format) {
498 struct roar_audio_info * info = &(handle->stream.info);
499
500 switch (format) {
501  case AFMT_S8:
502    info->bits  = 8;
503    info->codec = ROAR_CODEC_PCM_S_LE;
504   break;
505  case AFMT_U8:
506    info->bits  = 8;
507    info->codec = ROAR_CODEC_PCM_U_LE;
508   break;
509  case AFMT_S16_BE:
510    info->bits  = 16;
511    info->codec = ROAR_CODEC_PCM_S_BE;
512   break;
513  case AFMT_S16_LE:
514    info->bits  = 16;
515    info->codec = ROAR_CODEC_PCM_S_LE;
516   break;
517  case AFMT_U16_BE:
518    info->bits  = 16;
519    info->codec = ROAR_CODEC_PCM_U_BE;
520   break;
521  case AFMT_U16_LE:
522    info->bits  = 16;
523    info->codec = ROAR_CODEC_PCM_U_LE;
524   break;
525#ifdef AFMT_S32_BE
526  case AFMT_S32_BE:
527    info->bits  = 32;
528    info->codec = ROAR_CODEC_PCM_S_BE;
529   break;
530#endif
531#ifdef AFMT_S32_LE
532  case AFMT_S32_LE:
533    info->bits  = 32;
534    info->codec = ROAR_CODEC_PCM_S_LE;
535   break;
536#endif
537  case AFMT_A_LAW:
538    info->bits  = 8;
539    info->codec = ROAR_CODEC_ALAW;
540   break;
541  case AFMT_MU_LAW:
542    info->bits  = 8;
543    info->codec = ROAR_CODEC_MULAW;
544   break;
545#ifdef AFMT_VORBIS
546  case AFMT_VORBIS:
547    info->codec = ROAR_CODEC_OGG_VORBIS;
548   break;
549#endif
550  default:
551    ROAR_DBG("_ioctl_stream_format(*): unsupported format");
552    errno = ENOSYS;
553    return -1;
554   break;
555 }
556
557 return 0;
558}
559
560static inline int _ioctl_stream_format_list (void) {
561 int format = 0;
562
563 format |= AFMT_S8;
564 format |= AFMT_U8;
565
566 format |= AFMT_S16_BE;
567 format |= AFMT_S16_LE;
568
569 format |= AFMT_U16_BE;
570 format |= AFMT_U16_LE;
571
572#ifdef AFMT_S32_BE
573 format |= AFMT_S32_BE;
574#endif
575#ifdef AFMT_S32_LE
576 format |= AFMT_S32_LE;
577#endif
578
579 format |= AFMT_A_LAW;
580 format |= AFMT_MU_LAW;
581
582#ifdef AFMT_VORBIS
583 format |= AFMT_VORBIS;
584#endif
585
586 return format;
587}
588
589// -------------------------------------
590// mixer ioctls:
591// -------------------------------------
592
593static int _ioctl_mixer (struct handle * handle, long unsigned int req, void * vp) {
594 mixer_info * info;
595 int channels;
596 struct roar_mixer_settings mixer;
597 int o_w    =  0;
598 int o_sid  = -1;
599 int * ip   = vp;
600#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
601 char * name = NULL;
602#endif
603
604#if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES)
605 switch (req) {
606#if 0
607  case SNDCTL_MIX_DESCRIPTION: name = "SNDCTL_MIX_DESCRIPTION"; break;
608  case SNDCTL_MIX_ENUMINFO:    name = "SNDCTL_MIX_ENUMINFO";    break;
609  case SNDCTL_MIX_EXTINFO:     name = "SNDCTL_MIX_EXTINFO";     break;
610  case SNDCTL_MIX_NREXT:       name = "SNDCTL_MIX_NREXT";       break;
611  case SNDCTL_MIX_NRMIX:       name = "SNDCTL_MIX_NRMIX";       break;
612  case SNDCTL_MIX_READ:        name = "SNDCTL_MIX_READ";        break;
613  case SNDCTL_MIX_WRITE:       name = "SNDCTL_MIX_WRITE";       break;
614#endif
615//  case SOUND_MIXER_INFO:             name = "SOUND_MIXER_INFO";             break;
616  case SOUND_OLD_MIXER_INFO:         name = "SOUND_OLD_MIXER_INFO";         break;
617  case SOUND_MIXER_ACCESS:           name = "SOUND_MIXER_ACCESS";           break;
618  case SOUND_MIXER_AGC:              name = "SOUND_MIXER_AGC";              break;
619  case SOUND_MIXER_3DSE:             name = "SOUND_MIXER_3DSE";             break;
620  case SOUND_MIXER_GETLEVELS:        name = "SOUND_MIXER_GETLEVELS";        break;
621  case SOUND_MIXER_SETLEVELS:        name = "SOUND_MIXER_SETLEVELS";        break;
622  case SOUND_MIXER_PRIVATE1:         name = "SOUND_MIXER_PRIVATE1";         break;
623  case SOUND_MIXER_PRIVATE2:         name = "SOUND_MIXER_PRIVATE2";         break;
624  case SOUND_MIXER_PRIVATE3:         name = "SOUND_MIXER_PRIVATE3";         break;
625  case SOUND_MIXER_PRIVATE4:         name = "SOUND_MIXER_PRIVATE4";         break;
626  case SOUND_MIXER_PRIVATE5:         name = "SOUND_MIXER_PRIVATE5";         break;
627  case OSS_GETVERSION:               name = "OSS_GETVERSION";               break;
628//  case SOUND_MIXER_READ_CAPS:        name = "SOUND_MIXER_READ_CAPS";        break;
629  case SOUND_MIXER_READ_MUTE:        name = "SOUND_MIXER_READ_MUTE";        break;
630/*
631  case :     name = "";     break;
632  case :     name = "";     break;
633*/
634 }
635 if ( name != NULL ) {
636  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unspported mixer command %s", handle, req, ip, name);
637  ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
638  errno = ENOSYS;
639  return -1;
640 }
641#endif
642
643 switch (req) {
644  case SOUND_MIXER_READ_VOLUME:    o_w = 0; o_sid = _mix_settings.sid.volume;   break;
645  case SOUND_MIXER_READ_LINE:      o_w = 0; o_sid = _mix_settings.sid.line;     break;
646  case SOUND_MIXER_READ_LINE1:     o_w = 0; o_sid = _mix_settings.sid.line1;    break;
647  case SOUND_MIXER_READ_LINE2:     o_w = 0; o_sid = _mix_settings.sid.line2;    break;
648  case SOUND_MIXER_READ_LINE3:     o_w = 0; o_sid = _mix_settings.sid.line3;    break;
649#if 0
650  case SOUND_MIXER_READ_DIGITAL1:  o_w = 0; o_sid = _mix_settings.sid.digital1; break;
651  case SOUND_MIXER_READ_DIGITAL2:  o_w = 0; o_sid = _mix_settings.sid.digital2; break;
652  case SOUND_MIXER_READ_DIGITAL3:  o_w = 0; o_sid = _mix_settings.sid.digital3; break;
653#endif
654  case SOUND_MIXER_WRITE_VOLUME:   o_w = 1; o_sid = _mix_settings.sid.volume;   break;
655  case SOUND_MIXER_WRITE_LINE:     o_w = 1; o_sid = _mix_settings.sid.line;     break;
656  case SOUND_MIXER_WRITE_LINE1:    o_w = 1; o_sid = _mix_settings.sid.line1;    break;
657  case SOUND_MIXER_WRITE_LINE2:    o_w = 1; o_sid = _mix_settings.sid.line2;    break;
658  case SOUND_MIXER_WRITE_LINE3:    o_w = 1; o_sid = _mix_settings.sid.line3;    break;
659#if 0
660  case SOUND_MIXER_WRITE_DIGITAL1: o_w = 1; o_sid = _mix_settings.sid.digital1; break;
661  case SOUND_MIXER_WRITE_DIGITAL2: o_w = 1; o_sid = _mix_settings.sid.digital2; break;
662  case SOUND_MIXER_WRITE_DIGITAL3: o_w = 1; o_sid = _mix_settings.sid.digital3; break;
663#endif
664  // we handle PCM seperatly as we want to be abled to abled to handle it on a stream (not mixer), too:
665  case SOUND_MIXER_READ_PCM:
666    o_w = 0;
667    if ( handle->type == HT_STREAM ) {
668     o_sid = roar_stream_get_id(&(handle->stream));
669    } else {
670     o_sid = _mix_settings.sid.pcm;
671    }
672   break;
673  case SOUND_MIXER_WRITE_PCM:
674    o_w = 1;
675    if ( handle->type == HT_STREAM ) {
676     o_sid = roar_stream_get_id(&(handle->stream));
677    } else {
678     o_sid = _mix_settings.sid.pcm;
679    }
680   break;
681 }
682 if ( o_sid != -1 ) {
683  // set/get volume
684  if ( o_w ) {
685   mixer.scale    = 65535;
686   mixer.mixer[0] = ( *ip       & 0xFF)*65535/OSS_VOLUME_SCALE;
687   mixer.mixer[1] = ((*ip >> 8) & 0xFF)*65535/OSS_VOLUME_SCALE;
688   if ( roar_set_vol(&(handle->session->con), o_sid, &mixer, 2) == -1 ) {
689    errno = EIO;
690    return -1;
691   }
692   return 0;
693  } else {
694   if ( roar_get_vol(&(handle->session->con), o_sid, &mixer, &channels) == -1 ) {
695    errno = EIO;
696    return -1;
697   }
698   *ip = ((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale) | (((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale)<<8);
699   return 0;
700  }
701 }
702
703 switch (req) {
704  case SOUND_MIXER_READ_STEREODEVS: /* FIXME: check the streams for channel config */
705  case SOUND_MIXER_READ_DEVMASK:
706    *ip = 0;
707
708    if ( _mix_settings.sid.volume != -1 )
709     *ip |= SOUND_MASK_VOLUME;
710    if ( _mix_settings.sid.pcm != -1 )
711     *ip |= SOUND_MASK_PCM;
712    if ( _mix_settings.sid.line != -1 )
713     *ip |= SOUND_MASK_LINE;
714    if ( _mix_settings.sid.line1 != -1 )
715     *ip |= SOUND_MASK_LINE1;
716    if ( _mix_settings.sid.line2 != -1 )
717     *ip |= SOUND_MASK_LINE2;
718    if ( _mix_settings.sid.line3 != -1 )
719     *ip |= SOUND_MASK_LINE3;
720    if ( _mix_settings.sid.digital1 != -1 )
721#if 0
722     *ip |= SOUND_MASK_DIGITAL1;
723    if ( _mix_settings.sid.digital2 != -1 )
724     *ip |= SOUND_MASK_DIGITAL2;
725    if ( _mix_settings.sid.digital3 != -1 )
726     *ip |= SOUND_MASK_DIGITAL3;
727#endif
728
729    return 0;
730   break;
731  case SOUND_MIXER_READ_RECMASK:
732  case SOUND_MIXER_READ_RECSRC:
733    *ip = SOUND_MASK_VOLUME; // we can currently only read from mixer
734    return 0;
735   break;
736  case SOUND_MIXER_WRITE_RECSRC:
737    if ( *ip == SOUND_MASK_VOLUME ) {
738     return  0;
739    } else {
740     errno = ENOTSUP;
741     return -1;
742    }
743   break;
744  case SOUND_MIXER_READ_CAPS:
745    *ip = 0;
746    return 0;
747   break;
748  case SOUND_MIXER_INFO:
749    info = vp;
750    memset(info, 0, sizeof(*info));
751    strcpy(info->id, "RoarAudio");
752    strcpy(info->name, "RoarAudio");
753    return 0;
754   break;
755 }
756
757 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p): unknown mixer CTL", handle, req, ip);
758// _os.ioctl(-1, req, ip);
759 ROAR_DBG("_ioctl_mixer(handle=%p, req=%lu, ip=%p) = -1 // errno = ENOSYS", handle, req, ip);
760 errno = ENOSYS;
761 return -1;
762}
763
764// -------------------------------------
765// buffer size calculation:
766// -------------------------------------
767
768static size_t _get_stream_buffersize (struct handle * handle) {
769 if ( handle->stream_buffersize )
770  return handle->stream_buffersize;
771
772 return handle->stream_buffersize = handle->stream.info.rate     *
773                                    handle->stream.info.channels *
774                                    handle->stream.info.bits     / 800;
775}
776
777// -------------------------------------
778// emulated functions follow:
779// -------------------------------------
780
781int     open(const char *pathname, int flags, ...) {
782 int     ret;
783 mode_t  mode = 0;
784 va_list args;
785
786 _init();
787
788 ret = _open_file(pathname, flags);
789
790 switch (ret) {
791  case -2:       // continue as normal, use _op.open()
792   break;
793  case -1:       // pass error to caller
794    return -1;
795   break;
796  default:       // return successfully opened pointer to caller
797    return ret;
798   break;
799 }
800
801 if (flags & O_CREAT) {
802  va_start(args, flags);
803  mode = va_arg(args, mode_t);
804  va_end(args);
805 }
806
807 return _os.open(pathname, flags, mode);
808}
809
810int     close(int fd) {
811 struct pointer * pointer;
812 _init();
813
814 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
815  _close_pointer(pointer);
816  return 0;
817 }
818
819 return _os.close(fd);
820}
821
822ssize_t write(int fd, const void *buf, size_t count) {
823 struct pointer * pointer;
824 ssize_t ret;
825
826 _init();
827
828 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
829  if ( pointer->handle->type == HT_STREAM ) {
830   if ( pointer->handle->stream_opened == 0 ) {
831    if ( _open_stream(pointer->handle) == -1 ) {
832     errno = EIO;
833     return -1;
834    }
835   }
836   ret = roar_vio_write(&(pointer->handle->stream_vio), (char*)buf, count);
837   if ( ret > 0 )
838    pointer->handle->writec += ret;
839   return ret;
840  } else {
841   errno = EINVAL;
842   return -1;
843  }
844 }
845
846 return _os.write(fd, buf, count);
847}
848
849ssize_t read(int fd, void *buf, size_t count) {
850 struct pointer * pointer;
851 ssize_t ret;
852
853 _init();
854
855 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) {
856  if ( pointer->handle->type == HT_STREAM ) {
857   if ( pointer->handle->stream_opened == 0 ) {
858    if ( _open_stream(pointer->handle) == -1 ) {
859     errno = EIO;
860     return -1;
861    }
862   }
863   ret = roar_vio_read(&(pointer->handle->stream_vio), buf, count);
864   if ( ret > 0 )
865    pointer->handle->readc += ret;
866   return ret;
867  } else {
868   errno = EINVAL;
869   return -1;
870  }
871 }
872
873 return _os.read(fd, buf, count);
874}
875
876off_t lseek(int fildes, off_t offset, int whence) {
877 struct pointer * pointer;
878
879 _init();
880
881 if ( (pointer = _get_pointer_by_fh(fildes)) != NULL ) {
882  if ( pointer->handle->type == HT_DMX ) {
883   switch (whence) {
884    case SEEK_SET:
885      pointer->handle->pos  = offset;
886     break;
887    case SEEK_CUR:
888      pointer->handle->pos += offset;
889     break;
890    case SEEK_END:
891    default:
892      errno = EINVAL;
893      return -1;
894     break;
895   }
896   return pointer->handle->pos;
897  } else {
898   errno = EINVAL;
899   return -1;
900  }
901 }
902
903 return _os.lseek(fildes, offset, whence);
904}
905
906IOCTL() {
907 map_args;
908 struct pointer * pointer;
909 struct handle  * handle;
910 int * ip = NULL;
911 audio_buf_info * bi;
912 count_info     * ci;
913#ifdef __FIXME__
914 char * nosys_reqname = NULL;
915#endif
916#ifdef va_argp
917 va_list args;
918#endif
919
920 _init();
921
922// ROAR_DBG("ioctl(__fd=%i, __request=%lu) = ?", __fd, (long unsigned int) __request);
923
924#ifdef va_argp
925 va_start (args, ioctl_lastarg);
926 argp = va_arg (args, void *);
927 va_end (args);
928#endif
929
930// ROAR_DBG("ioctl(__fd=%i, __request=%lu): argp=%p", __fd, (long unsigned int) __request, argp);
931
932 if ( (pointer = _get_pointer_by_fh(__fd)) != NULL ) {
933  ip = argp;
934//  ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
935#ifdef __FIXME__
936  switch ((handle = pointer->handle)->type) {
937   case SOUND_PCM_READ_RATE: nosys_reqname = "SOUND_PCM_READ_RATE"; break;
938   case SOUND_PCM_READ_CHANNELS: nosys_reqname = "SOUND_PCM_READ_CHANNELS"; break;
939   case SOUND_PCM_READ_BITS: nosys_reqname = "SOUND_PCM_READ_BITS"; break;
940   case SOUND_PCM_READ_FILTER: nosys_reqname = "SOUND_PCM_READ_FILTER"; break;
941   case SNDCTL_COPR_RESET: nosys_reqname = "SNDCTL_COPR_RESET"; break;
942   case SNDCTL_COPR_LOAD: nosys_reqname = "SNDCTL_COPR_LOAD"; break;
943   case SNDCTL_COPR_HALT: nosys_reqname = "SNDCTL_COPR_HALT"; break;
944   case SNDCTL_COPR_RDATA: nosys_reqname = "SNDCTL_COPR_RDATA"; break;
945   case SNDCTL_COPR_RCODE: nosys_reqname = "SNDCTL_COPR_RCODE"; break;
946   case SNDCTL_COPR_WDATA: nosys_reqname = "SNDCTL_COPR_WDATA"; break;
947   case SNDCTL_COPR_WCODE: nosys_reqname = "SNDCTL_COPR_WCODE"; break;
948   case SNDCTL_COPR_RUN: nosys_reqname = "SNDCTL_COPR_RUN"; break;
949   case SNDCTL_COPR_SENDMSG: nosys_reqname = "SNDCTL_COPR_SENDMSG"; break;
950   case SNDCTL_COPR_RCVMSG: nosys_reqname = "SNDCTL_COPR_RCVMSG"; break;
951   case SNDCTL_DSP_SPEED: nosys_reqname = "SNDCTL_DSP_SPEED"; break;
952/*
953   case : nosys_reqname = ""; break;
954   case : nosys_reqname = ""; break;
955   case : nosys_reqname = ""; break;
956*/
957  }
958#endif
959  switch ((handle = pointer->handle)->type) {
960   case HT_STREAM:
961     switch (__request) {
962      case SNDCTL_DSP_RESET:
963      case SNDCTL_DSP_POST:
964      case SNDCTL_DSP_SETFRAGMENT: // any fragments should be ok for us...
965        return 0;
966       break;
967      case SNDCTL_DSP_SPEED:
968        handle->stream.info.rate = *ip;
969        ROAR_DBG("ioctl(__fd=%i, __request=%lu): rate=%i", __fd, (long unsigned int) __request, *ip);
970        return 0;
971       break;
972      case SNDCTL_DSP_CHANNELS:
973        handle->stream.info.channels = *ip;
974        ROAR_DBG("ioctl(__fd=%i, __request=%lu): channels=%i", __fd, (long unsigned int) __request, *ip);
975        return 0;
976       break;
977      case SNDCTL_DSP_STEREO:
978        handle->stream.info.channels = *ip ? 2 : 1;
979        return 0;
980       break;
981      case SNDCTL_DSP_GETBLKSIZE:
982        *ip = _get_stream_buffersize(handle);
983        return 0;
984       break;
985      case SNDCTL_DSP_SETFMT:
986        ROAR_DBG("ioctl(__fd=%i, __request=%lu): fmt=0x%x", __fd, (long unsigned int) __request, *ip);
987        return _ioctl_stream_format(handle, *ip);
988       break;
989      case SNDCTL_DSP_GETFMTS:
990//        ROAR_DBG("ioctl(__fd=%i, __request=%lu): ip=%p", __fd, (long unsigned int) __request, ip);
991        *ip = _ioctl_stream_format_list();
992        return 0;
993       break;
994      case SNDCTL_DSP_GETOSPACE:
995      case SNDCTL_DSP_GETISPACE:
996        bi = argp;
997        memset(bi, 0, sizeof(*bi));
998        bi->bytes      = _get_stream_buffersize(handle);
999        bi->fragments  = 1;
1000        bi->fragsize   = bi->bytes;
1001        bi->fragstotal = 1;
1002        return 0;
1003       break;
1004      case SNDCTL_DSP_GETOPTR:
1005        ci = argp;
1006        memset(ci, 0, sizeof(*ci));
1007        ci->bytes  = handle->writec;
1008        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
1009        ci->ptr    = 0;
1010        return 0;
1011       break;
1012      case SNDCTL_DSP_GETIPTR:
1013        ci = argp;
1014        memset(ci, 0, sizeof(*ci));
1015        ci->bytes  = handle->readc;
1016        ci->blocks = ci->bytes / _get_stream_buffersize(handle);
1017        ci->ptr    = 0;
1018        return 0;
1019       break;
1020#ifdef SNDCTL_DSP_GETPLAYVOL
1021      case SNDCTL_DSP_GETPLAYVOL:
1022        return _ioctl_mixer(handle, SOUND_MIXER_READ_PCM, argp);
1023       break;
1024#endif
1025#ifdef SNDCTL_DSP_SETPLAYVOL
1026      case SNDCTL_DSP_SETPLAYVOL:
1027        return _ioctl_mixer(handle, SOUND_MIXER_WRITE_PCM, argp);
1028       break;
1029#endif
1030      default:
1031#ifdef __FIXME__
1032        ROAR_DBG("ioctl(__fd=%i, __request=%lu (%s)) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request, nosys_reqname);
1033#else
1034        ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
1035#endif
1036        errno = ENOSYS;
1037        return -1;
1038     }
1039    break;
1040   case HT_MIXER:
1041     return _ioctl_mixer(handle, __request, argp);
1042    break;
1043   default:
1044     ROAR_DBG("ioctl(__fd=%i, __request=%lu): unknown handle type: no ioctl()s supported", __fd, (long unsigned int) __request);
1045     ROAR_DBG("ioctl(__fd=%i, __request=%lu) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request);
1046     errno = EINVAL;
1047     return -1;
1048    break;
1049  }
1050 }
1051
1052#ifdef IOCTL_IS_ALIAS
1053 errno = ENOSYS;
1054 return -1;
1055#else
1056 return _os.ioctl(__fd, __request, argp);
1057#endif
1058}
1059
1060#endif
1061
1062//ll
Note: See TracBrowser for help on using the repository browser.