Use a separate list for NULL-device extensions
[openal-soft.git] / Alc / solaris.c
blobe60ac9762d02710b56a82fb2dfa63e0e201ab04c
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 "config.h"
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <memory.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <math.h>
33 #include "alMain.h"
34 #include "AL/al.h"
35 #include "AL/alc.h"
37 #include <sys/audioio.h>
40 static const ALCchar solaris_device[] = "Solaris Software";
42 typedef struct {
43 int fd;
44 volatile int killNow;
45 ALvoid *thread;
47 ALubyte *mix_data;
48 int data_size;
49 } solaris_data;
52 static ALuint SolarisProc(ALvoid *ptr)
54 ALCdevice *pDevice = (ALCdevice*)ptr;
55 solaris_data *data = (solaris_data*)pDevice->ExtraData;
56 int remaining = 0;
57 ALint frameSize;
58 int wrote;
60 EnableRTPrio(RTPrioLevel);
62 frameSize = aluChannelsFromFormat(pDevice->Format) *
63 aluBytesFromFormat(pDevice->Format);
65 while(!data->killNow && pDevice->Connected)
67 ALint len = data->data_size;
68 ALubyte *WritePtr = data->mix_data;
70 aluMixData(pDevice, WritePtr, len/frameSize);
71 while(len > 0 && !data->killNow)
73 wrote = write(data->fd, WritePtr, len);
74 if(wrote < 0)
76 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
78 AL_PRINT("write failed: %s\n", strerror(errno));
79 aluHandleDisconnect(pDevice);
80 break;
83 Sleep(1);
84 continue;
87 len -= wrote;
88 WritePtr += wrote;
92 return 0;
96 static ALCboolean solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
98 char driver[64];
99 solaris_data *data;
101 strncpy(driver, GetConfigValue("solaris", "device", "/dev/audio"), sizeof(driver)-1);
102 driver[sizeof(driver)-1] = 0;
103 if(!deviceName)
104 deviceName = solaris_device;
105 else if(strcmp(deviceName, solaris_device) != 0)
106 return ALC_FALSE;
108 data = (solaris_data*)calloc(1, sizeof(solaris_data));
109 data->killNow = 0;
111 data->fd = open(driver, O_WRONLY);
112 if(data->fd == -1)
114 free(data);
115 if(errno != ENOENT)
116 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
117 return ALC_FALSE;
120 device->szDeviceName = strdup(deviceName);
121 device->ExtraData = data;
122 return ALC_TRUE;
125 static void solaris_close_playback(ALCdevice *device)
127 solaris_data *data = (solaris_data*)device->ExtraData;
129 close(data->fd);
130 free(data);
131 device->ExtraData = NULL;
134 static ALCboolean solaris_reset_playback(ALCdevice *device)
136 solaris_data *data = (solaris_data*)device->ExtraData;
137 audio_info_t info;
138 ALuint frameSize;
139 int numChannels;
141 AUDIO_INITINFO(&info);
143 switch(aluBytesFromFormat(device->Format))
145 case 1:
146 info.play.precision = 8;
147 info.play.encoding = AUDIO_ENCODING_LINEAR8;
148 break;
149 case 4:
150 switch(numChannels)
152 case 1: device->Format = AL_FORMAT_MONO16; break;
153 case 2: device->Format = AL_FORMAT_STEREO16; break;
154 case 4: device->Format = AL_FORMAT_QUAD16; break;
155 case 6: device->Format = AL_FORMAT_51CHN16; break;
156 case 7: device->Format = AL_FORMAT_61CHN16; break;
157 case 8: device->Format = AL_FORMAT_71CHN16; break;
159 /* fall-through */
160 case 2:
161 info.play.precision = 16;
162 info.play.encoding = AUDIO_ENCODING_LINEAR;
163 break;
164 default:
165 AL_PRINT("Unknown format: 0x%x\n", device->Format);
166 return ALC_FALSE;
169 numChannels = aluChannelsFromFormat(device->Format);
170 info.play.sample_rate = device->Frequency;
171 info.play.channels = numChannels;
173 frameSize = numChannels * aluBytesFromFormat(device->Format);
174 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
176 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
178 AL_PRINT("ioctl failed: %s\n", strerror(errno));
179 return ALC_FALSE;
182 if(aluChannelsFromFormat(device->Format) != info.play.channels)
184 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), info.play.channels);
185 return ALC_FALSE;
188 if(!((info.play.precision == 8 && aluBytesFromFormat(device->Format) == 1) ||
189 (info.play.precision == 16 && aluBytesFromFormat(device->Format) == 2)))
191 AL_PRINT("Could not set %d-bit output, got %d\n", aluBytesFromFormat(device->Format)*8, info.play.precision);
192 return ALC_FALSE;
195 device->Frequency = info.play.sample_rate;
196 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
198 data->data_size = device->UpdateSize * frameSize;
199 data->mix_data = calloc(1, data->data_size);
201 SetDefaultChannelOrder(device);
203 data->thread = StartThread(SolarisProc, device);
204 if(data->thread == NULL)
206 free(data->mix_data);
207 data->mix_data = NULL;
208 return ALC_FALSE;
211 return ALC_TRUE;
214 static void solaris_stop_playback(ALCdevice *device)
216 solaris_data *data = (solaris_data*)device->ExtraData;
218 if(!data->thread)
219 return;
221 data->killNow = 1;
222 StopThread(data->thread);
223 data->thread = NULL;
225 data->killNow = 0;
227 free(data->mix_data);
228 data->mix_data = NULL;
232 static ALCboolean solaris_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
234 (void)device;
235 (void)deviceName;
236 (void)frequency;
237 (void)format;
238 (void)SampleSize;
239 return ALC_FALSE;
242 static void solaris_close_capture(ALCdevice *device)
244 (void)device;
247 static void solaris_start_capture(ALCdevice *pDevice)
249 (void)pDevice;
252 static void solaris_stop_capture(ALCdevice *pDevice)
254 (void)pDevice;
257 static void solaris_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
259 (void)pDevice;
260 (void)pBuffer;
261 (void)lSamples;
264 static ALCuint solaris_available_samples(ALCdevice *pDevice)
266 (void)pDevice;
267 return 0;
271 BackendFuncs solaris_funcs = {
272 solaris_open_playback,
273 solaris_close_playback,
274 solaris_reset_playback,
275 solaris_stop_playback,
276 solaris_open_capture,
277 solaris_close_capture,
278 solaris_start_capture,
279 solaris_stop_capture,
280 solaris_capture_samples,
281 solaris_available_samples
284 void alc_solaris_init(BackendFuncs *func_list)
286 *func_list = solaris_funcs;
289 void alc_solaris_deinit(void)
293 void alc_solaris_probe(int type)
295 #ifdef HAVE_STAT
296 struct stat buf;
297 if(stat(GetConfigValue("solaris", "device", "/dev/audio"), &buf) != 0)
298 return;
299 #endif
301 if(type == DEVICE_PROBE)
302 AppendDeviceList(solaris_device);
303 else if(type == ALL_DEVICE_PROBE)
304 AppendAllDeviceList(solaris_device);