441
|
1
|
|
2 #include <string.h>
|
|
3 #include <sys/ioctl.h>
|
|
4 #include <sys/soundcard.h>
|
|
5 #include <fcntl.h>
|
|
6 #include <stdio.h>
|
|
7
|
|
8 #include "mixer.h"
|
|
9
|
|
10 #define DEV_MIXER "/dev/mixer"
|
|
11
|
|
12 char * mixer_device=NULL;
|
|
13 char * devname=NULL;
|
|
14 int mixer_usemaster=0;
|
|
15
|
|
16 void mixer_getvolume( int *l,int *r )
|
|
17 {
|
|
18 int fd,v,cmd,devs;
|
|
19
|
|
20 if ( !mixer_device ) devname=strdup( DEV_MIXER );
|
|
21 else devname=strdup( mixer_device );
|
|
22 fd=open( devname,O_RDONLY );
|
|
23 free( devname );
|
|
24 if ( fd != -1 )
|
|
25 {
|
|
26 ioctl( fd,SOUND_MIXER_READ_DEVMASK,&devs );
|
|
27 if ( ( devs & SOUND_MASK_PCM ) && ( mixer_usemaster==0 ) ) cmd=SOUND_MIXER_READ_PCM;
|
|
28 else
|
|
29 if ( ( devs & SOUND_MASK_VOLUME ) && ( mixer_usemaster==1 ) ) cmd=SOUND_MIXER_READ_VOLUME;
|
|
30 else
|
|
31 {
|
|
32 close( fd );
|
|
33 return;
|
|
34 }
|
|
35 ioctl( fd,cmd,&v );
|
|
36 *r=( v & 0xFF00 ) >> 8;
|
|
37 *l=( v & 0x00FF );
|
|
38 close( fd );
|
|
39 }
|
|
40 }
|
|
41
|
|
42 void mixer_setvolume( int l,int r )
|
|
43 {
|
|
44 int fd,v,cmd,devs;
|
|
45
|
|
46 if ( !mixer_device ) devname=strdup( DEV_MIXER );
|
|
47 else devname=strdup( mixer_device );
|
|
48 fd=open( devname,O_RDONLY );
|
|
49 free( devname );
|
|
50 if ( fd != -1 )
|
|
51 {
|
|
52 ioctl( fd,SOUND_MIXER_READ_DEVMASK,&devs );
|
|
53 if ( ( devs & SOUND_MASK_PCM ) && ( mixer_usemaster==0 ) ) cmd=SOUND_MIXER_WRITE_PCM;
|
|
54 else
|
|
55 if ( ( devs & SOUND_MASK_VOLUME ) && ( mixer_usemaster==1 ) ) cmd=SOUND_MIXER_WRITE_VOLUME;
|
|
56 else
|
|
57 {
|
|
58 close( fd );
|
|
59 return;
|
|
60 }
|
|
61 v=( r << 8 ) | l;
|
|
62 ioctl( fd,cmd,&v );
|
|
63 close( fd );
|
|
64 }
|
|
65 }
|
|
66
|
|
67 int mixer_l=0; int mixer_r=0;
|
|
68
|
|
69 void mixer_incvolume( void )
|
|
70 {
|
|
71 fprintf( stderr,"[mixer] inc.\n" );
|
|
72 mixer_getvolume( &mixer_l,&mixer_r );
|
|
73 if ( mixer_l < 100 ) mixer_l++;
|
|
74 if ( mixer_r < 100 ) mixer_r++;
|
|
75 mixer_setvolume( mixer_l,mixer_r );
|
|
76 }
|
|
77
|
|
78 void mixer_decvolume( void )
|
|
79 {
|
|
80 fprintf( stderr,"[mixer] dec.\n" );
|
|
81 mixer_getvolume( &mixer_l,&mixer_r );
|
|
82 if ( mixer_l > 0 ) mixer_l--;
|
|
83 if ( mixer_r > 0 ) mixer_r--;
|
|
84 mixer_setvolume( mixer_l,mixer_r );
|
|
85 }
|
|
86
|
|
87 int mixer_getbothvolume( void )
|
|
88 {
|
|
89 mixer_getvolume( &mixer_l,&mixer_r );
|
|
90 return ( mixer_l + mixer_r ) / 2;
|
|
91 }
|