summaryrefslogtreecommitdiff
path: root/sys/oss4/oss4-mixer-enum.c
blob: 4b64bb1fc47e1f52542b9dc6259ac7ab5f1e9d8d (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
/* GStreamer OSS4 mixer enumeration 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.
 */

/* An 'enum' in gnome-volume-control / GstMixer is represented by a
 * GstMixerOptions object
 */

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

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

#define NO_LEGACY_MIXER
#include "oss4-mixer.h"
#include "oss4-mixer-enum.h"
#include "oss4-soundcard.h"

GST_DEBUG_CATEGORY_EXTERN (oss4mixer_debug);
#define GST_CAT_DEFAULT oss4mixer_debug

static GList *gst_oss4_mixer_enum_get_values (GstMixerOptions * options);

/* GstMixerTrack is a plain GObject, so let's just use the GLib macro here */
G_DEFINE_TYPE (GstOss4MixerEnum, gst_oss4_mixer_enum, GST_TYPE_MIXER_OPTIONS);

static void
gst_oss4_mixer_enum_init (GstOss4MixerEnum * e)
{
  e->need_update = TRUE;
}

static void
gst_oss4_mixer_enum_dispose (GObject * obj)
{
  GstMixerOptions *options = GST_MIXER_OPTIONS (obj);

  /* our list is a flat list with constant strings, but the GstMixerOptions
   * dispose will try to g_free the contained strings, so clean up the list
   * before chaining up to GstMixerOptions */
  g_list_free (options->values);
  options->values = NULL;

  G_OBJECT_CLASS (gst_oss4_mixer_enum_parent_class)->dispose (obj);
}

static void
gst_oss4_mixer_enum_class_init (GstOss4MixerEnumClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstMixerOptionsClass *mixeroptions_class = (GstMixerOptionsClass *) klass;

  gobject_class->dispose = gst_oss4_mixer_enum_dispose;
  mixeroptions_class->get_values = gst_oss4_mixer_enum_get_values;
}

static GList *
gst_oss4_mixer_enum_get_values_locked (GstMixerOptions * options)
{
  GstOss4MixerEnum *e = GST_OSS4_MIXER_ENUM_CAST (options);
  GList *oldlist, *list = NULL;
  int i;

  /* if current list of values is empty, update/re-check in any case */
  if (!e->need_update && options->values != NULL)
    return options->values;

  GST_LOG_OBJECT (e, "updating available values for %s", e->mc->mixext.extname);

  for (i = 0; i < e->mc->mixext.maxvalue; ++i) {
    const gchar *s;

    s = g_quark_to_string (e->mc->enum_vals[i]);
    if (MIXEXT_ENUM_IS_AVAILABLE (e->mc->mixext, i)) {
      GST_LOG_OBJECT (e, "option '%s' is available", s);
      list = g_list_prepend (list, (gpointer) s);
    } else {
      GST_LOG_OBJECT (e, "option '%s' is currently not available", s);
    }
  }

  list = g_list_reverse (list);

  /* this is not thread-safe, but then the entire GstMixer API isn't really,
   * since we return foo->list and not a copy and don't take any locks, so
   * not much we can do here but pray; we're usually either called from _new()
   * or from within _get_values() though, so it should be okay. We could use
   * atomic ops here, but I'm not sure how much more that really buys us.*/
  oldlist = options->values;    /* keep window small */
  options->values = list;
  g_list_free (oldlist);

  e->need_update = FALSE;

  return options->values;
}

static GList *
gst_oss4_mixer_enum_get_values (GstMixerOptions * options)
{
  GstOss4MixerEnum *e = GST_OSS4_MIXER_ENUM (options);
  GList *list;

  /* we take the lock here mostly to serialise ioctls with the watch thread */
  GST_OBJECT_LOCK (e->mixer);

  list = gst_oss4_mixer_enum_get_values_locked (options);

  GST_OBJECT_UNLOCK (e->mixer);

  return list;
}

static const gchar *
gst_oss4_mixer_enum_get_current_value (GstOss4MixerEnum * e)
{
  const gchar *cur_val = NULL;

  if (e->mc->enum_vals != NULL && e->mc->last_val < e->mc->mixext.maxvalue) {
    cur_val = g_quark_to_string (e->mc->enum_vals[e->mc->last_val]);
  }

  return cur_val;
}

static gboolean
gst_oss4_mixer_enum_update_current (GstOss4MixerEnum * e)
{
  int cur = -1;

  if (!gst_oss4_mixer_get_control_val (e->mixer, e->mc, &cur))
    return FALSE;

  if (cur < 0 || cur >= e->mc->mixext.maxvalue) {
    GST_WARNING_OBJECT (e, "read value %d out of bounds [0-%d]", cur,
        e->mc->mixext.maxvalue - 1);
    e->mc->last_val = 0;
    return FALSE;
  }

  return TRUE;
}

gboolean
gst_oss4_mixer_enum_set_option (GstOss4MixerEnum * e, const gchar * value)
{
  GQuark q;
  int i;

  q = g_quark_try_string (value);
  if (q == 0) {
    GST_WARNING_OBJECT (e, "unknown option '%s'", value);
    return FALSE;
  }

  for (i = 0; i < e->mc->mixext.maxvalue; ++i) {
    if (q == e->mc->enum_vals[i])
      break;
  }

  if (i >= e->mc->mixext.maxvalue) {
    GST_WARNING_OBJECT (e, "option '%s' is not valid for this control", value);
    return FALSE;
  }

  GST_LOG_OBJECT (e, "option '%s' = %d", value, i);

  if (!MIXEXT_ENUM_IS_AVAILABLE (e->mc->mixext, i)) {
    GST_WARNING_OBJECT (e, "option '%s' is not selectable currently", value);
    return FALSE;
  }

  if (!gst_oss4_mixer_set_control_val (e->mixer, e->mc, i)) {
    GST_WARNING_OBJECT (e, "could not set option '%s' (%d)", value, i);
    return FALSE;
  }

  /* and re-read current value with sanity checks (or could just assign here) */
  gst_oss4_mixer_enum_update_current (e);

  return TRUE;
}

const gchar *
gst_oss4_mixer_enum_get_option (GstOss4MixerEnum * e)
{
  const gchar *cur_str = NULL;

  if (!gst_oss4_mixer_enum_update_current (e)) {
    GST_WARNING_OBJECT (e, "failed to read current value");
    return NULL;
  }

  cur_str = gst_oss4_mixer_enum_get_current_value (e);
  GST_LOG_OBJECT (e, "%s (%d)", GST_STR_NULL (cur_str), e->mc->last_val);
  return cur_str;
}

GstMixerTrack *
gst_oss4_mixer_enum_new (GstOss4Mixer * mixer, GstOss4MixerControl * mc)
{
  GstOss4MixerEnum *e;
  GstMixerTrack *track;

  e = g_object_new (GST_TYPE_OSS4_MIXER_ENUM, "untranslated-label",
      mc->mixext.extname, NULL);
  e->mixer = mixer;
  e->mc = mc;

  track = GST_MIXER_TRACK (e);

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

  track->num_channels = 0;
  track->min_volume = 0;
  track->max_volume = 0;

  (void) gst_oss4_mixer_enum_get_values_locked (GST_MIXER_OPTIONS (track));

  if (!gst_oss4_mixer_enum_update_current (e)) {
    GST_WARNING_OBJECT (track, "failed to read current value, returning NULL");
    g_object_unref (track);
    track = NULL;
  }

  GST_LOG_OBJECT (e, "current value: %d (%s)", e->mc->last_val,
      gst_oss4_mixer_enum_get_current_value (e));

  return track;
}

/* This is called from the watch thread */
void
gst_oss4_mixer_enum_process_change_unlocked (GstMixerTrack * track)
{
  GstOss4MixerEnum *e = GST_OSS4_MIXER_ENUM_CAST (track);

  gchar *cur;

  if (!e->mc->changed && !e->mc->list_changed)
    return;

  if (e->mc->list_changed) {
    gst_mixer_options_list_changed (GST_MIXER (e->mixer),
        GST_MIXER_OPTIONS (e));
  }

  GST_OBJECT_LOCK (e->mixer);
  cur = (gchar *) gst_oss4_mixer_enum_get_current_value (e);
  GST_OBJECT_UNLOCK (e->mixer);

  gst_mixer_option_changed (GST_MIXER (e->mixer), GST_MIXER_OPTIONS (e), cur);
}