summaryrefslogtreecommitdiff
path: root/sys/oss4/oss4-mixer-slider.c
blob: cdc910495afe2e261a8d076232f3f9bb1af64bcb (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
/* GStreamer OSS4 mixer slider control
 * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
 *
 * 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.
 */

/* A 'slider' in gnome-volume-control / GstMixer is represented by a
 * GstMixerTrack with one or more channels.
 *
 * A slider should be either flagged as INPUT or OUTPUT (mostly because of
 * gnome-volume-control being littered with g_asserts for everything it doesn't
 * expect).
 *
 * From mixertrack.h:
 * "Input tracks can have 'recording' enabled, which means that any input will
 * be hearable into the speakers that are attached to the output. Mute is
 * obvious."
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <gst/gst-i18n-plugin.h>

#define NO_LEGACY_MIXER
#include "oss4-mixer-slider.h"

GST_DEBUG_CATEGORY_EXTERN (oss4mixer_debug);
#define GST_CAT_DEFAULT oss4mixer_debug

/* GstMixerTrack is a plain GObject, so let's just use the GLib macro here */
G_DEFINE_TYPE (GstOss4MixerSlider, gst_oss4_mixer_slider, GST_TYPE_MIXER_TRACK);

static void
gst_oss4_mixer_slider_class_init (GstOss4MixerSliderClass * klass)
{
  /* nothing to do here */
}

static void
gst_oss4_mixer_slider_init (GstOss4MixerSlider * s)
{
  /* nothing to do here */
}

static int
gst_oss4_mixer_slider_pack_volume (GstOss4MixerSlider * s, const gint * volumes)
{
  int val = 0;

  switch (s->mc->mixext.type) {
    case MIXT_MONOSLIDER:
    case MIXT_MONOSLIDER16:
    case MIXT_SLIDER:
      val = volumes[0];
      break;
    case MIXT_STEREOSLIDER:
      val = ((volumes[1] & 0xff) << 8) | (volumes[0] & 0xff);
      break;
    case MIXT_STEREOSLIDER16:
      val = ((volumes[1] & 0xffff) << 16) | (volumes[0] & 0xffff);
      break;
    default:
      g_return_val_if_reached (0);
  }
  return val;
}

static void
gst_oss4_mixer_slider_unpack_volume (GstOss4MixerSlider * s, int v,
    gint * volumes)
{
  guint32 val;                  /* use uint so bitshifting the highest bit works right */

  val = (guint32) v;
  switch (s->mc->mixext.type) {
    case MIXT_SLIDER:
      volumes[0] = val;
      break;
    case MIXT_MONOSLIDER:
      /* oss repeats the value in the upper bits, as if it was stereo */
      volumes[0] = val & 0x00ff;
      break;
    case MIXT_MONOSLIDER16:
      /* oss repeats the value in the upper bits, as if it was stereo */
      volumes[0] = val & 0x0000ffff;
      break;
    case MIXT_STEREOSLIDER:
      volumes[0] = (val & 0x00ff);
      volumes[1] = (val & 0xff00) >> 8;
      break;
    case MIXT_STEREOSLIDER16:
      volumes[0] = (val & 0x0000ffff);
      volumes[1] = (val & 0xffff0000) >> 16;
      break;
    default:
      g_return_if_reached ();
  }
}

gboolean
gst_oss4_mixer_slider_get_volume (GstOss4MixerSlider * s, gint * volumes)
{
  GstMixerTrack *track = GST_MIXER_TRACK (s);
  int v = 0;

  /* if we're supposed to be muted, and don't have an actual mute control
   * (ie. 'simulate' the mute), then just return the volume as saved, not
   * the actually set volume which is most likely 0 */
  if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE) && !s->mc->mute) {
    volumes[0] = s->volumes[0];
    if (track->num_channels == 2)
      volumes[1] = s->volumes[1];
    return TRUE;
  }

  if (!gst_oss4_mixer_get_control_val (s->mixer, s->mc, &v))
    return FALSE;

  gst_oss4_mixer_slider_unpack_volume (s, v, volumes);

  if (track->num_channels > 1) {
    GST_LOG_OBJECT (s, "volume: left=%d, right=%d", volumes[0], volumes[1]);
  } else {
    GST_LOG_OBJECT (s, "volume: mono=%d", volumes[0]);
  }

  return TRUE;
}

gboolean
gst_oss4_mixer_slider_set_volume (GstOss4MixerSlider * s, const gint * volumes)
{
  GstMixerTrack *track = GST_MIXER_TRACK (s);
  int val = 0;

  /* if we're supposed to be muted, and are 'simulating' the mute because
   * we don't have a mute control, don't actually change the volume, just
   * save it as the new desired volume for later when we get unmuted again */
  if (!GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_NO_MUTE)) {
    if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE) && !s->mc->mute)
      goto done;
  }

  val = gst_oss4_mixer_slider_pack_volume (s, volumes);

  if (track->num_channels > 1) {
    GST_LOG_OBJECT (s, "left=%d, right=%d", volumes[0], volumes[1]);
  } else {
    GST_LOG_OBJECT (s, "mono=%d", volumes[0]);
  }

  if (!gst_oss4_mixer_set_control_val (s->mixer, s->mc, val))
    return FALSE;

done:

  s->volumes[0] = volumes[0];
  if (track->num_channels == 2)
    s->volumes[1] = volumes[1];

  return TRUE;
}

gboolean
gst_oss4_mixer_slider_set_record (GstOss4MixerSlider * s, gboolean record)
{
  /* There doesn't seem to be a way to do this using the OSS4 mixer API, so
   * just do nothing here for now. */
  return FALSE;
}

gboolean
gst_oss4_mixer_slider_set_mute (GstOss4MixerSlider * s, gboolean mute)
{
  GstMixerTrack *track = GST_MIXER_TRACK (s);
  gboolean ret;

  /* if the control does not support muting, then do not do anything */
  if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_NO_MUTE)) {
    return TRUE;
  }

  /* If we do not have a mute control, simulate mute (which is a bit broken,
   * since we can not differentiate between capture/playback volume etc., so
   * we just assume that setting the volume to 0 would be the same as muting
   * this control) */
  if (s->mc->mute == NULL) {
    int volume;

    if (mute) {
      /* make sure the current volume values get saved. */
      gst_oss4_mixer_slider_get_volume (s, s->volumes);
      volume = 0;
    } else {
      volume = gst_oss4_mixer_slider_pack_volume (s, s->volumes);
    }
    ret = gst_oss4_mixer_set_control_val (s->mixer, s->mc, volume);
  } else {
    ret = gst_oss4_mixer_set_control_val (s->mixer, s->mc->mute, !!mute);
  }

  if (mute) {
    track->flags |= GST_MIXER_TRACK_MUTE;
  } else {
    track->flags &= ~GST_MIXER_TRACK_MUTE;
  }

  return FALSE;
}

GstMixerTrack *
gst_oss4_mixer_slider_new (GstOss4Mixer * mixer, GstOss4MixerControl * mc)
{
  GstOss4MixerSlider *s;
  GstMixerTrack *track;
  gint volumes[2] = { 0, };

  s = g_object_new (GST_TYPE_OSS4_MIXER_SLIDER, "untranslated-label",
      mc->mixext.extname, NULL);

  track = GST_MIXER_TRACK (s);

  /* caller will set track->label and track->flags */

  s->mc = mc;
  s->mixer = mixer;

  /* we don't do value scaling but just present a scale of 0-maxvalue */
  track->min_volume = 0;
  track->max_volume = mc->mixext.maxvalue;

  switch (mc->mixext.type) {
    case MIXT_MONOSLIDER:
    case MIXT_MONOSLIDER16:
    case MIXT_SLIDER:
      track->num_channels = 1;
      break;
    case MIXT_STEREOSLIDER:
    case MIXT_STEREOSLIDER16:
      track->num_channels = 2;
      break;
    default:
      g_return_val_if_reached (NULL);
  }

  GST_LOG_OBJECT (track, "min=%d, max=%d, channels=%d", track->min_volume,
      track->max_volume, track->num_channels);

  if (!gst_oss4_mixer_slider_get_volume (s, volumes)) {
    GST_WARNING_OBJECT (track, "failed to read volume, returning NULL");
    g_object_unref (track);
    track = NULL;
  }

  return track;
}

/* This is called from the watch thread */
void
gst_oss4_mixer_slider_process_change_unlocked (GstMixerTrack * track)
{
  GstOss4MixerSlider *s = GST_OSS4_MIXER_SLIDER_CAST (track);

  if (s->mc->mute != NULL && s->mc->mute->changed) {
    gst_mixer_mute_toggled (GST_MIXER (s->mixer), track,
        !!s->mc->mute->last_val);
  } else {
    /* nothing to do here, since we don't/can't easily implement the record
     * flag */
  }

  if (s->mc->changed) {
    gint volumes[2] = { 0, 0 };

    gst_oss4_mixer_slider_unpack_volume (s, s->mc->last_val, volumes);

    /* if we 'simulate' the mute, update flag when the volume changes */
    if (s->mc->mute == NULL) {
      if (volumes[0] == 0 && volumes[1] == 0) {
        track->flags |= GST_MIXER_TRACK_MUTE;
      } else {
        track->flags &= ~GST_MIXER_TRACK_MUTE;
      }
    }

    gst_mixer_volume_changed (GST_MIXER (s->mixer), track, volumes);
  }
}