Add a method to deinitialize backends
[openal-soft.git] / Alc / solaris.c
blob5f0ebf17f6d7026ea9b19ef1932b888a73983746
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 char *solaris_device;
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 int wrote;
59 while(!data->killNow && !pDevice->Connected)
61 ALint len = data->data_size;
62 ALubyte *WritePtr = data->mix_data;
64 SuspendContext(NULL);
65 aluMixData(pDevice->Context, WritePtr, len, pDevice->Format);
66 ProcessContext(NULL);
68 while(len > 0 && !data->killNow)
70 wrote = write(data->fd, WritePtr, len);
71 if(wrote < 0)
73 if(errno != EAGAIN && errno != EWOULDBLOCK)
75 AL_PRINT("write failed: %s\n", strerror(errno));
76 aluHandleDisconnect(pDevice);
77 len = 0;
79 else
80 Sleep(1);
81 continue;
84 len -= wrote;
85 WritePtr += wrote;
89 return 0;
93 static ALCboolean solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
95 audio_info_t info;
96 ALuint frameSize;
97 char driver[64];
98 int numChannels;
99 solaris_data *data;
101 strncpy(driver, GetConfigValue("solaris", "device", "/dev/audio"), sizeof(driver)-1);
102 driver[sizeof(driver)-1] = 0;
103 if(deviceName)
105 if(strcmp(deviceName, solaris_device))
106 return ALC_FALSE;
107 device->szDeviceName = solaris_device;
109 else
110 device->szDeviceName = solaris_device;
112 data = (solaris_data*)calloc(1, sizeof(solaris_data));
113 data->killNow = 0;
115 data->fd = open(driver, O_WRONLY);
116 if(data->fd == -1)
118 free(data);
119 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
120 return ALC_FALSE;
123 numChannels = aluChannelsFromFormat(device->Format);
124 AUDIO_INITINFO(&info);
125 info.play.sample_rate = device->Frequency;
126 info.play.channels = numChannels;
128 switch(aluBytesFromFormat(device->Format))
130 case 1:
131 info.play.precision = 8;
132 info.play.encoding = AUDIO_ENCODING_LINEAR8;
133 break;
134 case 4:
135 switch(numChannels)
137 case 1: device->Format = AL_FORMAT_MONO16; break;
138 case 2: device->Format = AL_FORMAT_STEREO16; break;
139 case 4: device->Format = AL_FORMAT_QUAD16; break;
140 case 6: device->Format = AL_FORMAT_51CHN16; break;
141 case 7: device->Format = AL_FORMAT_61CHN16; break;
142 case 8: device->Format = AL_FORMAT_71CHN16; break;
144 /* fall-through */
145 case 2:
146 info.play.precision = 16;
147 info.play.encoding = AUDIO_ENCODING_LINEAR;
148 break;
149 default:
150 AL_PRINT("Unknown format?! %x\n", device->Format);
153 frameSize = numChannels * aluBytesFromFormat(device->Format);
154 info.play.buffer_size = device->BufferSize * frameSize;
156 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
158 AL_PRINT("ioctl failed: %s\n", strerror(errno));
159 close(data->fd);
160 free(data);
161 return ALC_FALSE;
164 if(aluChannelsFromFormat(device->Format) != info.play.channels)
166 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), info.play.channels);
167 close(data->fd);
168 free(data);
169 return ALC_FALSE;
172 if(!((info.play.precision == 8 && aluBytesFromFormat(device->Format) == 1) ||
173 (info.play.precision == 16 && aluBytesFromFormat(device->Format) == 2)))
175 AL_PRINT("Could not set %d-bit output, got %d\n", aluBytesFromFormat(device->Format)*8, info.play.precision);
176 close(data->fd);
177 free(data);
178 return ALC_FALSE;
181 device->Frequency = info.play.sample_rate;
182 device->UpdateSize = info.play.buffer_size / 4;
184 data->data_size = device->UpdateSize * frameSize;
185 data->mix_data = calloc(1, data->data_size);
187 device->ExtraData = data;
188 data->thread = StartThread(SolarisProc, device);
189 if(data->thread == NULL)
191 device->ExtraData = NULL;
192 free(data->mix_data);
193 free(data);
194 return ALC_FALSE;
197 return ALC_TRUE;
200 static void solaris_close_playback(ALCdevice *device)
202 solaris_data *data = (solaris_data*)device->ExtraData;
203 data->killNow = 1;
204 StopThread(data->thread);
206 close(data->fd);
208 free(data->mix_data);
209 free(data);
210 device->ExtraData = NULL;
213 static ALCboolean solaris_start_context(ALCdevice *device, ALCcontext *context)
215 device->Frequency = context->fFrequency;
216 return ALC_TRUE;
219 static void solaris_stop_context(ALCdevice *device, ALCcontext *context)
221 (void)device;
222 (void)context;
226 static ALCboolean solaris_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
228 (void)device;
229 (void)deviceName;
230 (void)frequency;
231 (void)format;
232 (void)SampleSize;
233 return ALC_FALSE;
236 static void solaris_close_capture(ALCdevice *device)
238 (void)device;
241 static void solaris_start_capture(ALCdevice *pDevice)
243 (void)pDevice;
246 static void solaris_stop_capture(ALCdevice *pDevice)
248 (void)pDevice;
251 static void solaris_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
253 (void)pDevice;
254 (void)pBuffer;
255 (void)lSamples;
258 static ALCuint solaris_available_samples(ALCdevice *pDevice)
260 (void)pDevice;
261 return 0;
265 BackendFuncs solaris_funcs = {
266 solaris_open_playback,
267 solaris_close_playback,
268 solaris_start_context,
269 solaris_stop_context,
270 solaris_open_capture,
271 solaris_close_capture,
272 solaris_start_capture,
273 solaris_stop_capture,
274 solaris_capture_samples,
275 solaris_available_samples
278 void alc_solaris_init(BackendFuncs *func_list)
280 *func_list = solaris_funcs;
282 solaris_device = AppendDeviceList("Solaris Software");
283 AppendAllDeviceList(solaris_device);
286 void alc_solaris_deinit(void)