summaryrefslogtreecommitdiff
path: root/plugins/media-keys/actions/acme-volume-oss.c
blob: 8c18c97ac1f30985d355e83a6ee3cb470730af21 (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
/* acme-volume-oss.c

   Copyright (C) 2002, 2003 Bastien Nocera

   The Gnome 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.

   The Gnome 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 the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Bastien Nocera <hadess@hadess.net>
 */

#include "config.h"
#include "acme-volume-oss.h"

#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif /* __NetBSD__ */

struct AcmeVolumeOssPrivate
{
	gboolean use_pcm;
	gboolean mixerpb;
	int volume;
	int saved_volume;
	gboolean pcm_avail;
	gboolean mute;
};

static GObjectClass *parent_class = NULL;

static int acme_volume_oss_get_volume (AcmeVolume *self);
static void acme_volume_oss_set_volume (AcmeVolume *self, int val);
static gboolean acme_volume_oss_mixer_check (AcmeVolumeOss *self, int fd);

G_DEFINE_TYPE (AcmeVolumeOss, acme_volume_oss, ACME_TYPE_VOLUME)

static void
acme_volume_oss_finalize (GObject *object)
{
	AcmeVolumeOss *self;

	g_return_if_fail (object != NULL);
	g_return_if_fail (ACME_IS_VOLUME_OSS (object));

	self = ACME_VOLUME_OSS (object);

	g_return_if_fail (self->_priv != NULL);
	g_free (self->_priv);

	G_OBJECT_CLASS (parent_class)->finalize (object);
}

static int
acme_volume_oss_vol_check (int volume)
{
	return CLAMP (volume, 0, 100);
}

static void
acme_volume_oss_set_mute (AcmeVolume *vol, gboolean val)
{
	AcmeVolumeOss *self = (AcmeVolumeOss *) vol;

	if (self->_priv->mute == FALSE)
	{
		self->_priv->saved_volume =
			acme_volume_oss_get_volume (vol);
		acme_volume_oss_set_volume (vol, 0);
		self->_priv->mute = TRUE;
	} else {
		acme_volume_oss_set_volume (vol, self->_priv->saved_volume);
		self->_priv->mute = FALSE;
	}
}

static gboolean
acme_volume_oss_get_mute (AcmeVolume *vol)
{
	AcmeVolumeOss *self = (AcmeVolumeOss *) vol;

	/* somebody else might have changed the volume */
	if ((self->_priv->mute == TRUE) && (self->_priv->volume != 0))
	{
		self->_priv->mute = FALSE;
	}

	return self->_priv->mute;
}

static int
acme_volume_oss_get_volume (AcmeVolume *vol)
{
	gint volume, r, l, fd;
	AcmeVolumeOss *self = (AcmeVolumeOss *) vol;

	fd  = open ("/dev/mixer", O_RDONLY);
	if (acme_volume_oss_mixer_check(self, fd) == FALSE)
	{
		volume = 0;
	} else {
		if (self->_priv->use_pcm && self->_priv->pcm_avail)
			ioctl (fd, MIXER_READ (SOUND_MIXER_PCM), &volume);
		else
			ioctl (fd, MIXER_READ (SOUND_MIXER_VOLUME), &volume);
		close (fd);

		r = (volume & 0xff);
		l = (volume & 0xff00) >> 8;
		volume = (r + l) / 2;
		volume = acme_volume_oss_vol_check (volume);
	}

	return volume;
}

static void
acme_volume_oss_set_volume (AcmeVolume *vol, int val)
{
	int fd, tvol, volume;
	AcmeVolumeOss *self = (AcmeVolumeOss *) vol;

	volume = acme_volume_oss_vol_check (val);

	fd = open ("/dev/mixer", O_RDONLY);
	if (acme_volume_oss_mixer_check (self, fd) == FALSE)
	{
		return;
	} else {
		tvol = (volume << 8) + volume;
		if (self->_priv->use_pcm && self->_priv->pcm_avail)
			ioctl (fd, MIXER_WRITE (SOUND_MIXER_PCM), &tvol);
		else
			ioctl (fd, MIXER_WRITE (SOUND_MIXER_VOLUME), &tvol);
		close (fd);
		self->_priv->volume = volume;
	}
}

static void
acme_volume_oss_init (AcmeVolumeOss *self)
{
	int fd;

	self->_priv = g_new0 (AcmeVolumeOssPrivate, 1);

	fd  = open ("/dev/mixer", O_RDONLY);
	if (acme_volume_oss_mixer_check(self, fd) == FALSE)
	{
		self->_priv->pcm_avail = FALSE;
	} else {
		int mask = 0;

		ioctl (fd, SOUND_MIXER_READ_DEVMASK, &mask);
		if (mask & ( 1 << SOUND_MIXER_PCM))
			self->_priv->pcm_avail = TRUE;
		else
			self->_priv->pcm_avail = FALSE;
		if (!(mask & ( 1 << SOUND_MIXER_VOLUME)))
			self->_priv->use_pcm = TRUE;

		close (fd);
	}
}

static void
acme_volume_oss_class_init (AcmeVolumeOssClass *klass)
{
	AcmeVolumeClass *volume_class = ACME_VOLUME_CLASS (klass);
	G_OBJECT_CLASS (klass)->finalize = acme_volume_oss_finalize;

	parent_class = g_type_class_peek_parent (klass);

	volume_class->set_volume = acme_volume_oss_set_volume;
	volume_class->get_volume = acme_volume_oss_get_volume;
	volume_class->set_mute = acme_volume_oss_set_mute;
	volume_class->get_mute = acme_volume_oss_get_mute;
}

static gboolean
acme_volume_oss_mixer_check (AcmeVolumeOss *self, int fd)
{
	gboolean retval;

	if (fd <0) {
		if (self->_priv->mixerpb == FALSE) {
			self->_priv->mixerpb = TRUE;
			//FIXME
			//volume_oss_fd_problem(self);
		}
	}
	retval = (!self->_priv->mixerpb);
	return retval;
}