Use -Wextra instead of -W for extra warnings
[openal-soft.git] / Alc / oss.c
blob1b8b084722845c1644491a8ab286eb9f103f7cd4
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
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.
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.
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 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <memory.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <math.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
35 #include <sys/soundcard.h>
38 * The OSS documentation talks about SOUND_MIXER_READ, but the header
39 * only contains MIXER_READ. Play safe. Same for WRITE.
41 #ifndef SOUND_MIXER_READ
42 #define SOUND_MIXER_READ MIXER_READ
43 #endif
44 #ifndef SOUND_MIXER_WRITE
45 #define SOUND_MIXER_WRITE MIXER_WRITE
46 #endif
48 static char *oss_device;
50 typedef struct {
51 int fd;
52 int killNow;
53 ALvoid *thread;
55 ALubyte *mix_data;
56 int data_size;
57 int silence;
58 } oss_data;
61 static int log2i(ALCuint x)
63 int y = 0;
64 while (x > 1)
66 x >>= 1;
67 y++;
69 return y;
73 static ALuint OSSProc(ALvoid *ptr)
75 ALCdevice *pDevice = (ALCdevice*)ptr;
76 oss_data *data = (oss_data*)pDevice->ExtraData;
77 int remaining;
78 int wrote;
80 while(!data->killNow)
82 SuspendContext(NULL);
83 aluMixData(pDevice->Context,data->mix_data,data->data_size,pDevice->Format);
84 ProcessContext(NULL);
86 remaining = data->data_size;
87 while(remaining > 0)
89 wrote = write(data->fd, data->mix_data+data->data_size-remaining, remaining);
90 if(wrote < 0)
92 AL_PRINT("write failed: %s\n", strerror(errno));
93 break;
95 if(wrote == 0)
97 usleep(1000);
98 continue;
100 remaining -= wrote;
104 return 0;
107 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
109 int numFragmentsLogSize;
110 int log2FragmentSize;
111 unsigned int periods;
112 audio_buf_info info;
113 char driver[64];
114 int numChannels;
115 oss_data *data;
116 int ossFormat;
117 int ossSpeed;
118 char *err;
119 int i;
121 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
122 driver[sizeof(driver)-1] = 0;
123 if(deviceName)
125 if(strcmp(deviceName, oss_device))
126 return ALC_FALSE;
129 if(deviceName)
130 strcpy(device->szDeviceName, deviceName);
131 else
132 strcpy(device->szDeviceName, oss_device);
134 data = (oss_data*)calloc(1, sizeof(oss_data));
135 data->killNow = 0;
137 data->fd = open(driver, O_WRONLY);
138 if(data->fd == -1)
140 free(data);
141 return ALC_FALSE;
144 switch(device->Format)
146 case AL_FORMAT_MONO8:
147 case AL_FORMAT_STEREO8:
148 case AL_FORMAT_QUAD8:
149 data->silence = 0x80;
150 ossFormat = AFMT_U8;
151 break;
152 case AL_FORMAT_MONO16:
153 case AL_FORMAT_STEREO16:
154 case AL_FORMAT_QUAD16:
155 data->silence = 0;
156 ossFormat = AFMT_S16_NE;
157 break;
158 default:
159 ossFormat = -1;
160 AL_PRINT("Unknown format?! %x\n", device->Format);
163 periods = GetConfigValueInt("oss", "periods", 4);
164 if((int)periods < 0)
165 periods = 4;
166 numChannels = device->Channels;
167 ossSpeed = device->Frequency;
168 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
170 /* according to the OSS spec, 16 bytes are the minimum */
171 if (log2FragmentSize < 4)
172 log2FragmentSize = 4;
173 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
175 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
176 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
177 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
178 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
179 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
180 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
182 AL_PRINT("%s failed: %s\n", err, strerror(i));
183 close(data->fd);
184 free(data);
185 return ALC_FALSE;
187 #undef ok
189 device->Channels = numChannels;
190 device->Frequency = ossSpeed;
191 device->Format = 0;
192 if(ossFormat == AFMT_U8)
194 if(device->Channels == 1)
196 device->Format = AL_FORMAT_MONO8;
197 device->FrameSize = 1;
199 else if(device->Channels == 2)
201 device->Format = AL_FORMAT_STEREO8;
202 device->FrameSize = 2;
204 else if(device->Channels == 4)
206 device->Format = AL_FORMAT_QUAD8;
207 device->FrameSize = 4;
210 else if(ossFormat == AFMT_S16_NE)
212 if(device->Channels == 1)
214 device->Format = AL_FORMAT_MONO16;
215 device->FrameSize = 2;
217 else if(device->Channels == 2)
219 device->Format = AL_FORMAT_STEREO16;
220 device->FrameSize = 4;
222 else if(device->Channels == 4)
224 device->Format = AL_FORMAT_QUAD16;
225 device->FrameSize = 8;
229 if(!device->Format)
231 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
232 close(data->fd);
233 free(data);
234 return ALC_FALSE;
237 device->UpdateFreq = info.fragsize / device->FrameSize;
239 data->data_size = device->UpdateFreq * device->FrameSize;
240 data->mix_data = calloc(1, data->data_size);
242 device->MaxNoOfSources = 256;
244 device->ExtraData = data;
245 data->thread = StartThread(OSSProc, device);
246 if(data->thread == NULL)
248 device->ExtraData = NULL;
249 free(data->mix_data);
250 free(data);
251 return ALC_FALSE;
254 return ALC_TRUE;
257 static void oss_close_playback(ALCdevice *device)
259 oss_data *data = (oss_data*)device->ExtraData;
260 data->killNow = 1;
261 StopThread(data->thread);
263 close(data->fd);
265 free(data->mix_data);
266 free(data);
267 device->ExtraData = NULL;
271 static ALCboolean oss_open_capture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
273 (void)pDevice;
274 (void)deviceName;
275 (void)frequency;
276 (void)format;
277 (void)SampleSize;
278 return ALC_FALSE;
281 static void oss_close_capture(ALCdevice *pDevice)
283 (void)pDevice;
286 static void oss_start_capture(ALCdevice *pDevice)
288 (void)pDevice;
291 static void oss_stop_capture(ALCdevice *pDevice)
293 (void)pDevice;
296 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
298 (void)pDevice;
299 (void)pBuffer;
300 (void)lSamples;
303 static ALCuint oss_available_samples(ALCdevice *pDevice)
305 (void)pDevice;
306 return 0;
310 BackendFuncs oss_funcs = {
311 oss_open_playback,
312 oss_close_playback,
313 oss_open_capture,
314 oss_close_capture,
315 oss_start_capture,
316 oss_stop_capture,
317 oss_capture_samples,
318 oss_available_samples
321 void alc_oss_init(BackendFuncs *func_list)
323 *func_list = oss_funcs;
325 oss_device = AppendDeviceList("OSS Software");
326 AppendAllDeviceList(oss_device);