File: volume.c

package info (click to toggle)
mctools-lite 970129-15
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,684 kB
  • ctags: 1,659
  • sloc: ansic: 16,035; sh: 167; makefile: 69
file content (98 lines) | stat: -rw-r--r-- 2,368 bytes parent folder | download | duplicates (6)
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
/* Copyright (C) 1994 - 1996 
            Olav Woelfelschneider (wosch@rbg.informatik.th-darmstadt.de)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "../config.h"
#include "volume.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sys/errno.h>

static int mixer = -1;
extern char *mixer_name;
extern const unsigned char *sources[];
extern char *myname;

int mix_open(int request) {
  if (mixer<0) {
    int devmask;
    mixer = open(mixer_name, O_RDONLY);
    if (mixer<0) return -1;

    if (ioctl(mixer, SOUND_MIXER_READ_DEVMASK, &devmask)<0) {
      fprintf(stderr, _("%s: failed to read mixer capabilities.\n"), myname);
      goto fail;
    }

    if (!(devmask & (1<<request))) {
      fprintf(stderr, _("%s: mixer id `%s' not supported by your system.\n"),
	      myname, sources[request]);
      errno=ENODEV;
      goto fail;
    }

  }
  return mixer;

fail:
  close(mixer);
  return -1;
}

void mix_close() {
  if (mixer>=0) {
    close(mixer);
    mixer=-1;
  }
}

int mix_set_volume(int req, int *left, int *right) {
  int res,n;

  if (mixer>=0) {
    if ((*left>=0) && (*right>=0)) {
      n = (*left & 0x7F) + ((*right & 0x7F)<<8);
      res=ioctl(mixer, MIXER_WRITE(req), &n);
    } else {
      res=0;
    }
    if (res>=0) {
      res=ioctl(mixer,MIXER_READ(req), &n);
      *left = n&0x7F;
      *right = (n>>8)&0x7F;
    }
  } else
    res=-1;
  if (res<0) *left = *right = 0;
  return res;
}

int mix_get_volume(int req, int *left, int *right) {
  int res,n;

  if (mixer>=0) {
    res=ioctl(mixer, MIXER_READ(req), &n);
    *left = n&0x7F;
    *right = (n>>8)&0x7F;
  } else
    res=-1;
  if (res<0) *left = *right = 0;
  return res;
}