sys/oss/: Actually use the 'oss' debug category we register.
[platform/upstream/gst-plugins-good.git] / sys / oss / gstossmixertrack.c
1 /* GStreamer OSS Mixer implementation
2  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * gstossmixer.c: mixer interface implementation for OSS
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/ioctl.h>
33 #include <sys/soundcard.h>
34
35 #include <gst/gst-i18n-plugin.h>
36
37 #include "gstossmixertrack.h"
38
39 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
40 #define GST_CAT_DEFAULT oss_debug
41
42 #define MASK_BIT_IS_SET(mask, bit) \
43   (mask & (1 << bit))
44
45 G_DEFINE_TYPE (GstOssMixerTrack, gst_ossmixer_track, GST_TYPE_MIXER_TRACK);
46
47 static void
48 gst_ossmixer_track_class_init (GstOssMixerTrackClass * klass)
49 {
50   /* nop */
51 }
52
53 static void
54 gst_ossmixer_track_init (GstOssMixerTrack * track)
55 {
56   track->lvol = track->rvol = 0;
57   track->track_num = 0;
58 }
59
60 static const gchar **labels = NULL;
61
62 /* three functions: firstly, OSS has the nasty habit of inserting
63  * spaces in the labels, we want to get rid of them. Secondly,
64  * i18n is impossible with OSS' way of providing us with mixer
65  * labels, so we make a 'given' list of i18n'ed labels. Thirdly, I
66  * personally don't like the "1337" names that OSS gives to their
67  * labels ("Vol", "Mic", "Rec"), I'd rather see full names. */
68
69 static void
70 fill_labels (void)
71 {
72   gint i, pos;
73   gchar *origs[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS;
74   struct
75   {
76     gchar *given, *wanted;
77   }
78   cases[] = {
79     /* Note: this list is simply ripped from soundcard.h. For
80      * some people, some values might be missing (3D surround,
81      * etc.) - feel free to add them. That's the reason why
82      * I'm doing this in such a horribly complicated way. */
83     {
84     "Vol  ", _("Volume")}
85     , {
86     "Bass ", _("Bass")}
87     , {
88     "Trebl", _("Treble")}
89     , {
90     "Synth", _("Synth")}
91     , {
92     "Pcm  ", _("PCM")}
93     , {
94     "Spkr ", _("Speaker")}
95     , {
96     "Line ", _("Line-in")}
97     , {
98     "Mic  ", _("Microphone")}
99     , {
100     "CD   ", _("CD")}
101     , {
102     "Mix  ", _("Mixer")}
103     , {
104     "Pcm2 ", _("PCM-2")}
105     , {
106     "Rec  ", _("Record")}
107     , {
108     "IGain", _("In-gain")}
109     , {
110     "OGain", _("Out-gain")}
111     , {
112     "Line1", _("Line-1")}
113     , {
114     "Line2", _("Line-2")}
115     , {
116     "Line3", _("Line-3")}
117     , {
118     "Digital1", _("Digital-1")}
119     , {
120     "Digital2", _("Digital-2")}
121     , {
122     "Digital3", _("Digital-3")}
123     , {
124     "PhoneIn", _("Phone-in")}
125     , {
126     "PhoneOut", _("Phone-out")}
127     , {
128     "Video", _("Video")}
129     , {
130     "Radio", _("Radio")}
131     , {
132     "Monitor", _("Monitor")}
133     , {
134     NULL, NULL}
135   };
136
137   labels = g_malloc (sizeof (gchar *) * SOUND_MIXER_NRDEVICES);
138
139   for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
140     for (pos = 0; cases[pos].given != NULL; pos++) {
141       if (!strcmp (cases[pos].given, origs[i])) {
142         labels[i] = g_strdup (cases[pos].wanted);
143         break;
144       }
145     }
146     if (cases[pos].given == NULL)
147       labels[i] = g_strdup (origs[i]);
148   }
149 }
150
151 GstMixerTrack *
152 gst_ossmixer_track_new (gint mixer_fd,
153     gint track_num, gint max_chans, gint flags)
154 {
155   GstOssMixerTrack *osstrack;
156   GstMixerTrack *track;
157   gint volume;
158
159   if (!labels)
160     fill_labels ();
161
162   osstrack = g_object_new (GST_TYPE_OSSMIXER_TRACK, NULL);
163   track = GST_MIXER_TRACK (osstrack);
164   track->label = g_strdup (labels[track_num]);
165   track->num_channels = max_chans;
166   track->flags = flags;
167   track->min_volume = 0;
168   track->max_volume = 100;
169   osstrack->track_num = track_num;
170
171   /* volume */
172   if (ioctl (mixer_fd, MIXER_READ (osstrack->track_num), &volume) < 0) {
173     g_warning ("Error getting device (%d) volume: %s",
174         osstrack->track_num, strerror (errno));
175     volume = 0;
176   }
177   osstrack->lvol = (volume & 0xff);
178   if (track->num_channels == 2) {
179     osstrack->rvol = ((volume >> 8) & 0xff);
180   }
181
182   return track;
183 }