File: oss_mixer.c

package info (click to toggle)
gkrellm-volume 2.1.13-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 304 kB
  • ctags: 241
  • sloc: ansic: 1,929; makefile: 68
file content (177 lines) | stat: -rw-r--r-- 4,631 bytes parent folder | download | duplicates (2)
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
/* GKrellM Volume plugin
 |  Copyright (C) 1999-2000 Sjoerd Simons
 |
 |  Author:  Sjoerd Simons  sjoerd@luon.net
 |
 |  This program is free software which I release under the GNU General Public
 |  License. You may redistribute and/or modify this program under the terms
 |  of that 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.
 |
 |  To get a copy of the GNU General Puplic License,  write to the
 |  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <glob.h>

#include <sys/param.h>
#if defined(__FreeBSD__) && __FreeBSD_version < 500000
  #include <machine/soundcard.h>
#else
  #include <sys/soundcard.h>
#endif

#include "mixer.h"
#include "oss_mixer.h"

#define OSSMIXER(x) ((oss_mixer_t *)x->priv)
static mixer_ops_t * get_mixer_ops(void);


/* tries to open a mixer device, returns NULL on error or otherwise an mixer_t
 * struct */
static mixer_t *
oss_mixer_open(char *id) {
  mixer_t *result;
  oss_mixer_t *ossresult;
  int fd,devices,nr,i;
#ifdef SOUND_MIXER_INFO
  mixer_info minfo;
#endif
  char *sound_labels[] = SOUND_DEVICE_LABELS;

  if ((fd = open(id,O_RDWR)) == -1) return NULL;
  if ( (ioctl(fd,SOUND_MIXER_READ_DEVMASK,&devices) < 0)) {
    close(fd);
    return NULL;
  }

#ifdef SOUND_MIXER_INFO
  if ((ioctl(fd,SOUND_MIXER_INFO,&minfo) < 0)) {
    close(fd);
    return NULL;
  }
#endif

  result = malloc(sizeof(mixer_t));
#ifdef SOUND_MIXER_INFO
  result->name = strdup(minfo.name);
#else
  result->name = strdup(id);
#endif

  /* get the nr of devices */
  nr = 0;
  for (i = 0 ; i < SOUND_MIXER_NRDEVICES; i++) { if (devices & (1<<i)) nr++; }

  result->nrdevices = nr;

  result->dev_realnames = malloc(nr * sizeof(char *));

  result->dev_names = malloc(nr * sizeof(char*));
  memset(result->dev_names,0,nr * sizeof(char *));

  ossresult = malloc(sizeof(oss_mixer_t));
  ossresult->fd = fd;
  ossresult->table = malloc(nr * sizeof(int));

  result->priv = ossresult;
  result->ops = get_mixer_ops();

  nr = 0;
  for (i = 0 ; i < SOUND_MIXER_NRDEVICES; i++)
    if (devices & (1<<i)) {
      ossresult->table[nr] = i;
      result->dev_realnames[nr] = strdup(sound_labels[i]);
      nr++;
    }
  return result;
}

static void 
oss_mixer_close(mixer_t *mixer) {
  int i;
  close(OSSMIXER(mixer)->fd);
  for (i=0;i < mixer->nrdevices ; i++) {
    free(mixer->dev_names[i]);
    free(mixer->dev_realnames[i]);
  }
  free(mixer->dev_names);
  free(mixer->dev_realnames);
  free(OSSMIXER(mixer)->table);
  free(mixer->priv);
  free(mixer);
}

/* get the full scale of a device and get/set the volume */
static long
oss_mixer_device_get_fullscale(mixer_t *mixer,int devid) {
  return 100;
}

static void 
oss_mixer_device_get_volume(mixer_t *mixer, int devid,int *left,int *right) {
  long amount;
  ioctl(OSSMIXER(mixer)->fd,MIXER_READ(OSSMIXER(mixer)->table[devid]),&amount);
  *left = amount & 0xff;
  *right = amount >> 8;
}

static void  
oss_mixer_device_set_volume(mixer_t *mixer, int devid,int left,int right) {
  long amount = (right << 8) + (left & 0xff);
  ioctl(OSSMIXER(mixer)->fd,MIXER_WRITE(OSSMIXER(mixer)->table[devid]),&amount);
}

static mixer_idz_t *
oss_mixer_get_id_list(void) {
  mixer_idz_t *result = NULL;
  char *device[] = { "/dev/mixer*","/dev/sound/mixer*"};
  glob_t pglob;
  int i,n;

  for (n = 0; n < (sizeof(device)/sizeof(char *)); n++) {
    if (glob(device[n],0,NULL,&pglob) == 0) {
      for (i = 0; i < pglob.gl_pathc; i++) { 
        char *rc,buffer[PATH_MAX];
        rc = realpath(pglob.gl_pathv[i],buffer);

        if (rc == NULL) continue;
        result = mixer_id_list_add(rc,result); 
      }
      globfree(&pglob);
    }
  }
  return result;
}

static mixer_ops_t oss_mixer_ops = {
  .mixer_get_id_list = oss_mixer_get_id_list,
  .mixer_open = oss_mixer_open,
  .mixer_close = oss_mixer_close,
  .mixer_device_get_fullscale = oss_mixer_device_get_fullscale,
  .mixer_device_get_volume = oss_mixer_device_get_volume,
  .mixer_device_set_volume = oss_mixer_device_set_volume
};

static mixer_ops_t *
get_mixer_ops(void) {
  return &oss_mixer_ops;
}

mixer_ops_t *
init_oss_mixer(void) {
  return get_mixer_ops();
}