summaryrefslogtreecommitdiff
path: root/esddsp.c
blob: d3c6ea9b4d69143242869957ac46f9acd26cc671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/* Evil evil evil hack to get OSS apps to cooperate with esd
 * Copyright (C) 1998, 1999 Manish Singh <yosh@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* #define DSP_DEBUG */

/* This lets you run multiple instances of x11amp by setting the X11AMPNUM
   environment variable. Only works on glibc2.
 */
/* #define MULTIPLE_X11AMP */

#if defined(__GNUC__) && !defined(__STRICT_ANSI__)

#define _GNU_SOURCE 1

#ifdef DSP_DEBUG
#define DPRINTF(format, args...)	printf(format, ## args)
#else
#define DPRINTF(format, args...)
#endif

#include "config.h"

#include <dlfcn.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

#ifdef HAVE_MACHINE_SOUNDCARD_H
#  include <machine/soundcard.h>
#else
#  ifdef HAVE_SOUNDCARD_H
#    include <soundcard.h>
#  else
#    include <sys/soundcard.h>
#  endif
#endif

#include "esd.h"

/* BSDI has this functionality, but not define :() */
#if defined(RTLD_NEXT)
#define REAL_LIBC RTLD_NEXT
#else
#define REAL_LIBC ((void *) -1L)
#endif

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__bsdi__)
typedef unsigned long request_t;
#else
typedef int request_t;
#endif

static int sndfd = -1, mixfd = -1;
static int settings = 0, done = 0;

static char *ident = NULL, *mixer = NULL;
static int use_mixer = 0;

/*
 * memory mapping emulation
 */
static int mmapemu = 0;
static count_info mmapemu_ocount;
static char *mmapemu_obuffer = NULL;
static int mmapemu_osize;
static long long mmapemu_bytes_per_sec;
struct timeval mmapemu_last_flush;

#define MMAPEMU_FRAGSTOTAL 16
#define MMAPEMU_FRAGSIZE ESD_BUF_SIZE

#define OSS_VOLUME_BASE 50

#define ESD_VOL_TO_OSS(left, right) (short int)			\
    (((OSS_VOLUME_BASE * (right) / ESD_VOLUME_BASE) << 8) |	\
      (OSS_VOLUME_BASE * (left)  / ESD_VOLUME_BASE))

#define OSS_VOL_TO_ESD_LEFT(vol) 				\
    (ESD_VOLUME_BASE * (vol & 0xff) / OSS_VOLUME_BASE)
#define OSS_VOL_TO_ESD_RIGHT(vol) 				\
    (ESD_VOLUME_BASE * ((vol >> 8) & 0xff) / OSS_VOLUME_BASE)

static void
get_volume (int *left, int *right)
{
  int vol;

  if (read (mixfd, &vol, sizeof (vol)) != sizeof (vol))
    *left = *right = ESD_VOLUME_BASE;
  else
    {
      *left  = OSS_VOL_TO_ESD_LEFT  (vol);
      *right = OSS_VOL_TO_ESD_RIGHT (vol);
    }
}

static void
set_volume (int left, int right)
{
  int vol = ESD_VOL_TO_OSS (left, right);

  write (mixfd, &vol, sizeof (vol));
}


pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static void
dsp_init (void)
{
  pthread_mutex_lock(&mutex);
  if (!ident)
    {
      const char *str;
     
      str = getenv ("ESDDSP_NAME");
      ident = malloc (ESD_NAME_MAX+1);
      strncpy (ident, (str ? str : "esddsp"), ESD_NAME_MAX);
      ident[ESD_NAME_MAX] = '\0';

      str = getenv("ESDDSP_MMAP");
      mmapemu = str && !strcmp(str,"1");

      if (getenv ("ESDDSP_MIXER"))
	{
	  use_mixer = 1;

	  str = getenv ("HOME");
	  if (str)
 	    {
	      mixer = malloc (strlen (str) + strlen (ident) + 10);
	      sprintf (mixer, "%s/.esddsp_%s", str, ident);
	    }
	  else
	    {
	      fprintf (stderr, "esddsp: can't get home directory\n");
	      exit (1);
	    }

	  DPRINTF ("mixer settings file: %s\n", mixer);
	}
    }
  pthread_mutex_unlock(&mutex);
}

static void
mix_init (int *esd, int *player)
{
  esd_info_t *all_info;
  esd_player_info_t *player_info;

  if (*esd < 0 && (*esd = esd_open_sound (NULL)) < 0)
    return;
    
  if (*player < 0)
    {
      all_info = esd_get_all_info (*esd);
      if (all_info)
	{
	  for (player_info = all_info->player_list; player_info;
	       player_info = player_info->next)
	    if (!strcmp(player_info->name, ident))
	      {
		*player = player_info->source_id;
		break;
	      }

	  esd_free_all_info (all_info);
	}
    }
}

static void mmapemu_flush(void)
{
  struct timeval current_time;
  long long usec_since_last_flush;
  long long bytes_to_flush;
  int frags_to_flush;

  gettimeofday(&current_time, NULL);
  usec_since_last_flush = 1000000 * (current_time.tv_sec - mmapemu_last_flush.tv_sec) + (current_time.tv_usec - mmapemu_last_flush.tv_usec);

  bytes_to_flush = (mmapemu_bytes_per_sec * usec_since_last_flush) / 1000000;
  frags_to_flush  = bytes_to_flush / MMAPEMU_FRAGSIZE;

  if (frags_to_flush > MMAPEMU_FRAGSTOTAL)
    frags_to_flush = MMAPEMU_FRAGSTOTAL;

  if (frags_to_flush > 0)
    mmapemu_last_flush = current_time;

  while (frags_to_flush-- > 0)
    {
      write(sndfd, &mmapemu_obuffer[mmapemu_ocount.ptr], MMAPEMU_FRAGSIZE);
      mmapemu_ocount.ptr += MMAPEMU_FRAGSIZE;
      mmapemu_ocount.ptr %= mmapemu_osize;
      mmapemu_ocount.blocks++;
      mmapemu_ocount.bytes += MMAPEMU_FRAGSIZE;
    }
}

static int
open_wrapper (int (*func) (const char *, int, mode_t),
	      const char *pathname, int flags, ...)
{
  va_list args;
  mode_t mode;

  dsp_init ();

  va_start (args, flags);
  if (sizeof (mode_t) < sizeof (int))
	  mode = va_arg (args, int);
  else
	  mode = va_arg (args, mode_t);
  va_end (args);

  if (!strcmp (pathname, "/dev/dsp"))
    {
      if (!getenv ("ESPEAKER") && !mmapemu)
	{
          int ret;

	  flags |= O_NONBLOCK;
	  if ((ret = (*func) (pathname, flags, mode)) >= 0)
	    return ret;
	}

      DPRINTF ("hijacking /dev/dsp open, and taking it to esd...\n");
      settings = done = 0;
      return (sndfd = esd_open_sound (NULL));
    }
  else if (use_mixer && !strcmp (pathname, "/dev/mixer"))
    {
      DPRINTF ("hijacking /dev/mixer open, and taking it to esd...\n");
      return (mixfd = (*func) (mixer, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
    }
  else
    return (*func) (pathname, flags, mode);
}

int
open (const char *pathname, int flags, ...)
{
  static int (*func) (const char *, int, mode_t) = NULL;
  va_list args;
  mode_t mode;

  DPRINTF ("open\n");

  if (!func) 
    {
      func = (int (*) (const char *, int, mode_t)) dlsym (REAL_LIBC, "open");
      if (!func) 
	{
	  fprintf(stderr, "esddsp: error: Cannot find symbol 'open'\n");
	  errno = ENXIO;
	  return -1;
	}
    }

  va_start (args, flags);
  mode = va_arg (args, mode_t);
  va_end (args);

  return open_wrapper(func, pathname, flags, mode);
}

int
open64 (const char *pathname, int flags, ...)
{
  static int (*func) (const char *, int, mode_t) = NULL;
  va_list args;
  mode_t mode;

  DPRINTF ("open64\n");

  if (!func) 
    {
      func = (int (*) (const char *, int, mode_t)) dlsym (REAL_LIBC, "open64");
      if (!func) 
	{
	  fprintf(stderr, "esddsp: error: Cannot find symbol 'open64'\n");
	  errno = ENXIO;
	  return -1;
	}
    }

  va_start (args, flags);
  mode = va_arg (args, mode_t);
  va_end (args);

  return open_wrapper(func, pathname, flags, mode);
}

static FILE *
fopen_wrapper (FILE * (*func) (const char *, const char *),
	       const char *pathname, const char *mode)
{
  dsp_init ();

  if (!strcmp (pathname, "/dev/dsp"))
    {
      if (!getenv ("ESPEAKER") && !mmapemu)
	{
          FILE *ret;

	  if ((ret = (*func) (pathname, mode)) != NULL)
	    return ret;
	}

      DPRINTF ("hijacking /dev/dsp fopen, and taking it to esd...\n");
      settings = done = 0;
      return fdopen((sndfd = esd_open_sound (NULL)), mode);
    }
  else if (use_mixer && !strcmp (pathname, "/dev/mixer"))
    {
      FILE *ret;
      DPRINTF ("hijacking /dev/mixer fopen, and taking it to esd...\n");

      if ((ret = (*func) (pathname, mode)) != NULL)
	{
	  mixfd = fileno(ret);
	  return ret;
	}
      else
        return NULL;
    }
  else
    return (*func) (pathname, mode);
}

FILE *
fopen (const char *path, const char *mode)
{
  static FILE * (*func) (const char *, const char *) = NULL;

  DPRINTF ("fopen\n");

  if (!func) 
    {
      func = (FILE * (*) (const char *, const char *)) dlsym (REAL_LIBC, "fopen");
      if (!func) 
	{
	  fprintf(stderr, "esddsp: error: Cannot find symbol 'fopen'\n");
	  return NULL;
	}
    }

  return fopen_wrapper(func, path, mode);
}

FILE *
fopen64 (const char *path, const char *mode)
{
  static FILE * (*func) (const char *, const char *) = NULL;

  DPRINTF ("fopen64\n");

  if (!func) 
    {
      func = (FILE * (*) (const char *, const char *)) dlsym (REAL_LIBC, "fopen64");
      if (!func) 
	{
	  fprintf(stderr, "esddsp: error: Cannot find symbol 'fopen64'\n");
	  return NULL;
	}
    }

  return fopen_wrapper(func, path, mode);
}

static int
dspctl (int fd, request_t request, void *argp)
{
  static esd_format_t fmt = ESD_STREAM | ESD_PLAY | ESD_MONO;
  static int speed;

  int *arg = (int *) argp;

  DPRINTF ("hijacking /dev/dsp ioctl, and sending it to esd "
	   "(%d : %x - %p)\n", fd, request, argp);
  
  switch (request)
    {
    case SNDCTL_DSP_RESET:
    case SNDCTL_DSP_POST:
      break;

    case SNDCTL_DSP_SETFMT:
      fmt |= (*arg & 0x30) ? ESD_BITS16 : ESD_BITS8;
      settings |= 1;
      break;

    case SNDCTL_DSP_SPEED:
      speed = *arg;
      settings |= 2;
      break;

    case SNDCTL_DSP_STEREO:
      fmt &= ~ESD_MONO;
      fmt |= (*arg) ? ESD_STEREO : ESD_MONO;
      break;

    case SNDCTL_DSP_GETBLKSIZE:
      if (mmapemu)
      	*arg = MMAPEMU_FRAGSIZE;
      else
      	*arg = ESD_BUF_SIZE;
     
      break;

    case SNDCTL_DSP_GETFMTS:
      *arg = 0x38;
      break;

#ifdef SNDCTL_DSP_GETCAPS
    case SNDCTL_DSP_GETCAPS:
      if (mmapemu)
	*arg = DSP_CAP_MMAP | DSP_CAP_TRIGGER | DSP_CAP_REALTIME;
      else
	*arg = 0;
      break;
#endif

    case SNDCTL_DSP_GETOSPACE:
    case SNDCTL_DSP_GETISPACE:
      {
	audio_buf_info *bufinfo = (audio_buf_info *) argp;
	if (mmapemu)
	  {
	    /* Number of fragments allocated for buffering */
	    bufinfo->fragstotal = MMAPEMU_FRAGSTOTAL;
	    /* Size of a fragment in bytes (same as SNDCTL_DSP_GETBLKSIZE) */
	    bufinfo->fragsize = MMAPEMU_FRAGSIZE;
	    /* Number of bytes that can be read or written immediately without blocking */
	    bufinfo->bytes = MMAPEMU_FRAGSIZE;
	    /* Number of full fragments that can be read or written without blocking */
	    bufinfo->fragments = 1;
	  }
	else
	  {
	    /* Number of fragments allocated for buffering */
	    bufinfo->fragstotal = 1;
	    /* Size of a fragment in bytes (same as SNDCTL_DSP_GETBLKSIZE) */
	    bufinfo->fragsize = ESD_BUF_SIZE;
	    /* Number of bytes that can be read or written immediately without blocking */
	    bufinfo->bytes = ESD_BUF_SIZE;
	    /* Number of full fragments that can be read or written without blocking */
	    bufinfo->fragments = 1;
	  }
      }
      break;

    case SNDCTL_DSP_GETOPTR:
      if (mmapemu)
        {
	  DPRINTF("esddsp: mmapemu getoptr\n");
	  mmapemu_flush();
	  *((count_info *)arg) = mmapemu_ocount;
	  mmapemu_ocount.blocks = 0;
	}
      else
        {
	  DPRINTF ("esddsp: SNDCTL_DSP_GETOPTR unsupported\n");
	}
      break;

    default:
      DPRINTF ("unhandled /dev/dsp ioctl (%x - %p)\n", request, argp);
      break;
    }

  if (settings == 3 && !done)
    {
      int proto = ESD_PROTO_STREAM_PLAY;

      done = 1;

      if (write (sndfd, &proto, sizeof (proto)) != sizeof (proto))
        return -1;
      if (write (sndfd, &fmt, sizeof (fmt)) != sizeof (fmt))
        return -1;
      if (write (sndfd, &speed, sizeof (speed)) != sizeof (speed))
        return -1;
      if (write (sndfd, ident, ESD_NAME_MAX) != ESD_NAME_MAX)
        return -1;

      if (mmapemu)
	{
	  mmapemu_ocount.ptr=mmapemu_ocount.blocks=mmapemu_ocount.bytes=0;
	  mmapemu_bytes_per_sec = speed * ((fmt & ESD_BITS16) ? 2 : 1) * ((fmt & ESD_STEREO) ? 2 : 1);
	  gettimeofday(&mmapemu_last_flush, NULL);
	}

      fmt = ESD_STREAM | ESD_PLAY | ESD_MONO;
      speed = 0;

      if (use_mixer)
	{
  	  int esd = -1, player = -1;
	  int left, right;

	  while (player < 0)
	    mix_init (&esd, &player);

	  get_volume (&left, &right);

	  DPRINTF ("panning %d - %d %d\n", player, left, right);
	  esd_set_stream_pan (esd, player, left, right);

	  esd_close(esd);
	}
    }

  return 0;
}

int
mixctl (int fd, request_t request, void *argp)
{
  static int esd = -1, player = -1;
  static int left, right;

  int *arg = (int *) argp;

  DPRINTF ("hijacking /dev/mixer ioctl, and sending it to esd "
	   "(%d : %x - %p)\n", fd, request, argp);

  switch (request)
    {
    case SOUND_MIXER_READ_DEVMASK:
      *arg = 5113;
      break;

    case SOUND_MIXER_READ_PCM:
      mix_init (&esd, &player);

      if (player > 0)
	{
	  esd_info_t *all_info;

	  all_info  = esd_get_all_info (esd);

	  esd_close(esd);

	  if (all_info)
	    {
	      esd_player_info_t *player_info;

	      for (player_info = all_info->player_list; player_info;
		   player_info = player_info->next)
		if (player_info->source_id == player)
		  {
		    *arg = ESD_VOL_TO_OSS (player_info->left_vol_scale,
					   player_info->right_vol_scale);
		  }

	      esd_free_all_info (all_info);
	    }
	  else
	    return -1;
	}
      else
	{
          get_volume (&left, &right);
          esd_close(esd);
	  *arg = ESD_VOL_TO_OSS (left, right);
	}

      break;

    case SOUND_MIXER_WRITE_PCM:
      mix_init (&esd, &player);

      left  = OSS_VOL_TO_ESD_LEFT  (*arg);
      right = OSS_VOL_TO_ESD_RIGHT (*arg);

      set_volume (left, right);

      if (player > 0)
	{
	  DPRINTF ("panning %d - %d %d\n", player, left, right);
	  esd_set_stream_pan (esd, player, left, right);
	}

      esd_close(esd);

      break;

    default:
      DPRINTF ("unhandled /dev/mixer ioctl (%x - %p)\n", request, argp);
      break;
    }

  return 0;
}

int
ioctl (int fd, request_t request, ...)
{
  static int (*func) (int, request_t, void *) = NULL;
  va_list args;
  void *argp;

  if (!func)                                                                    
    func = (int (*) (int, request_t, void *)) dlsym (REAL_LIBC, "ioctl");             
  va_start (args, request);
  argp = va_arg (args, void *);
  va_end (args);

  if (fd == sndfd)
    return dspctl (fd, request, argp);
  else if (fd == mixfd) {
    if(use_mixer)
      return mixctl (fd, request, argp);
  } else { /* (fd != sndfd && fd != mixfd) */
        return (*func) (fd, request, argp); 
      }
  return 0;
}

int
close (int fd)
{
  static int (*func) (int) = NULL;

  if (!func)
    func = (int (*) (int)) dlsym (REAL_LIBC, "close");

  if (fd == sndfd)
    sndfd = -1;
  else if (fd == mixfd)
    mixfd = -1;
 
  return (*func) (fd);
}

void *
mmap (void  *start,  size_t length, int prot, int flags, int fd, off_t offset)
{
  static void *(*func) (void *, size_t, int, int, int, off_t) = NULL;
  
  if (!func)
    func = (void * (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap");

  if(fd != sndfd || sndfd == -1)
    return (*func)(start,length,prot,flags,fd,offset);
  else
    {
      DPRINTF ("esddsp: mmap - start = %x, length = %d, prot = %d\n",
	       start, length, prot);
      DPRINTF ("      flags = %d, fd = %d, offset = %d\n",flags, fd,offset);
      if(mmapemu)
	{
	  mmapemu_osize = length;
	  mmapemu_obuffer = malloc(length);
	  mmapemu_ocount.ptr = mmapemu_ocount.blocks = mmapemu_ocount.bytes = 0;
	  return mmapemu_obuffer;
	}
      else DPRINTF ("esddsp: /dev/dsp mmap (unsupported, try -m option)...\n");
    }
  return (void *)-1;
}

void *
mmap64 (void  *start,  size_t length, int prot, int flags, int fd, off64_t offset)
{
  static void *(*func) (void *, size_t, int, int, int, off64_t) = NULL;
  
  if (!func)
    func = (void * (*) (void *, size_t, int, int, int, off64_t)) dlsym (REAL_LIBC, "mmap64");

  if(fd != sndfd || sndfd == -1)
    return (*func)(start,length,prot,flags,fd,offset);
  else
    {
      DPRINTF ("esddsp: mmap64 - start = %x, length = %d, prot = %d\n",
	       start, length, prot);
      DPRINTF ("      flags = %d, fd = %d, offset = %lld\n",flags, fd,offset);
      if(mmapemu)
	{
	  mmapemu_osize = length;
	  mmapemu_obuffer = malloc(length);
	  mmapemu_ocount.ptr = mmapemu_ocount.blocks = mmapemu_ocount.bytes = 0;
	  return mmapemu_obuffer;
	}
      else DPRINTF ("esddsp: /dev/dsp mmap64 (unsupported, try -m option)...\n");
    }
  return (void *)-1;
}

int
munmap (void *start, size_t length)
{
  static int (*func) (void *, size_t) = NULL;

  if (!func)
    func = (int (*) (void *, size_t)) dlsym (REAL_LIBC, "munmap");

  if(start != mmapemu_obuffer || mmapemu_obuffer == 0)
    return (*func)(start,length);

  DPRINTF ("esddsp: /dev/dsp munmap...\n");
  mmapemu_obuffer = NULL;
  free(start);

  return 0;
}

#ifdef MULTIPLE_X11AMP

#include <socketbits.h>
#include <sys/param.h>
#include <sys/un.h>

#define ENVSET "X11AMPNUM"

int
unlink (const char *filename)
{
  static int (*func) (const char *) = NULL;
  const char *num;

  if (!func)
    func = (int (*) (const char *)) dlsym (REAL_LIBC, "unlink");

  if (!strcmp (filename, "/tmp/X11Amp_CTRL") && (num = getenv (ENVSET)))
    {
      char buf[PATH_MAX] = "/tmp/X11Amp_CTRL";
      strncat (buf, num, (PATH_MAX - strlen(buf) - 1));
      return (*func) (buf); 
    }
  else
    return (*func) (filename);
}

typedef int (*sa_func_t) (int, struct sockaddr *, int);

static int
sockaddr_mangle (sa_func_t func, int fd, struct sockaddr *addr, int len)
{
  const char *num;

  if (!strcmp (((struct sockaddr_un *) addr)->sun_path, "/tmp/X11Amp_CTRL")
      && (num = getenv(ENVSET)))
    {
      int ret;
      char buf[PATH_MAX] = "/tmp/X11Amp_CTRL";

      struct sockaddr_un *new_addr = malloc (len);

      strncat (buf, num, (PATH_MAX - strlen(buf) - 1));
      memcpy (new_addr, addr, len);
      strncpy (new_addr->sun_path, buf, strlen(new_addr->sun_path));

      ret = (*func) (fd, (struct sockaddr *) new_addr, len);

      free (new_addr);
      return ret;
    } 
  else
    return (*func) (fd, addr, len);
}

int
bind (int fd, struct sockaddr *addr, int len)
{
  static sa_func_t func = NULL;

  if (!func)
    func = (sa_func_t) dlsym (REAL_LIBC, "bind");
  return sockaddr_mangle (func, fd, addr, len);
}

int
connect (int fd, struct sockaddr *addr, int len)
{
  static sa_func_t func = NULL;

  if (!func)
    func = (sa_func_t) dlsym (REAL_LIBC, "connect");
  return sockaddr_mangle (func, fd, addr, len);
}

#endif /* MULTIPLE_X11AMP */

#else /* __GNUC__ */

void
nogcc (void)
{
  ident = NULL;
}

#endif