Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / sys / oss4 / oss4-mixer-switch.c
1 /* GStreamer OSS4 mixer on/off switch control
2  * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* A simple ON/OFF 'switch' in gnome-volume-control / GstMixer is represented
21  * by a GstMixerTrack with no channels.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst-i18n-plugin.h>
29
30 #define NO_LEGACY_MIXER
31 #include "oss4-mixer-switch.h"
32 #include "oss4-soundcard.h"
33
34 GST_DEBUG_CATEGORY_EXTERN (oss4mixer_debug);
35 #define GST_CAT_DEFAULT oss4mixer_debug
36
37 /* GstMixerTrack is a plain GObject, so let's just use the GLib macro here */
38 G_DEFINE_TYPE (GstOss4MixerSwitch, gst_oss4_mixer_switch, GST_TYPE_MIXER_TRACK);
39
40 static void
41 gst_oss4_mixer_switch_class_init (GstOss4MixerSwitchClass * klass)
42 {
43   /* nothing to do here */
44 }
45
46 static void
47 gst_oss4_mixer_switch_init (GstOss4MixerSwitch * s)
48 {
49   /* nothing to do here */
50 }
51
52 gboolean
53 gst_oss4_mixer_switch_set (GstOss4MixerSwitch * s, gboolean disabled)
54 {
55   GstMixerTrack *track;
56   int newval;
57
58   track = GST_MIXER_TRACK (s);
59
60   newval = disabled ? GST_MIXER_TRACK_MUTE : 0;
61
62   if (newval == (track->flags & GST_MIXER_TRACK_MUTE)) {
63     GST_LOG_OBJECT (s, "switch is already %d, doing nothing", newval);
64     return TRUE;
65   }
66
67   if (!gst_oss4_mixer_set_control_val (s->mixer, s->mc, !disabled)) {
68     GST_WARNING_OBJECT (s, "could not set switch to %d", !disabled);
69     return FALSE;
70   }
71
72   if (disabled) {
73     track->flags |= GST_MIXER_TRACK_MUTE;
74   } else {
75     track->flags &= ~GST_MIXER_TRACK_MUTE;
76   }
77
78   GST_LOG_OBJECT (s, "set switch to %d", newval);
79
80   return TRUE;
81 }
82
83 gboolean
84 gst_oss4_mixer_switch_get (GstOss4MixerSwitch * s, gboolean * disabled)
85 {
86   GstMixerTrack *track;
87   int enabled = -1;
88
89   track = GST_MIXER_TRACK (s);
90
91   if (!gst_oss4_mixer_get_control_val (s->mixer, s->mc, &enabled)
92       || (enabled < 0)) {
93     GST_WARNING_OBJECT (s, "could not get switch state");
94     return FALSE;
95   }
96
97   if (enabled) {
98     track->flags &= ~GST_MIXER_TRACK_MUTE;
99   } else {
100     track->flags |= GST_MIXER_TRACK_MUTE;
101   }
102   *disabled = (enabled == 0);
103
104   return TRUE;
105 }
106
107 GstMixerTrack *
108 gst_oss4_mixer_switch_new (GstOss4Mixer * mixer, GstOss4MixerControl * mc)
109 {
110   GstOss4MixerSwitch *s;
111   GstMixerTrack *track;
112   int cur = -1;
113
114   s = g_object_new (GST_TYPE_OSS4_MIXER_SWITCH, "untranslated-label",
115       mc->mixext.extname, NULL);
116
117   s->mixer = mixer;
118   s->mc = mc;
119
120   track = GST_MIXER_TRACK (s);
121
122   /* caller will set track->label and track->flags */
123
124   track->num_channels = 0;
125   track->min_volume = 0;
126   track->max_volume = 0;
127
128   if (!gst_oss4_mixer_get_control_val (s->mixer, s->mc, &cur) || cur < 0)
129     return NULL;
130
131   if (cur) {
132     track->flags &= ~GST_MIXER_TRACK_MUTE;
133   } else {
134     track->flags |= GST_MIXER_TRACK_MUTE;
135   }
136
137   return track;
138 }
139
140 /* This is called from the watch thread */
141 void
142 gst_oss4_mixer_switch_process_change_unlocked (GstMixerTrack * track)
143 {
144   GstOss4MixerSwitch *s = GST_OSS4_MIXER_SWITCH_CAST (track);
145
146   if (!s->mc->changed)
147     return;
148
149   gst_mixer_mute_toggled (GST_MIXER (s->mixer), track, !s->mc->last_val);
150 }