Initialize OpenAL with a constructor call instead of first-use
[openal-soft.git] / Alc / solaris.c
blobb56596087e2ea0a86d1be31392d860b5f2a34fc9
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 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)
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 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
116 return ALC_FALSE;
119 numChannels = aluChannelsFromFormat(device->Format);
120 AUDIO_INITINFO(&info);
121 info.play.sample_rate = device->Frequency;
122 info.play.channels = numChannels;
124 switch(aluBytesFromFormat(device->Format))
126 case 1:
127 info.play.precision = 8;
128 info.play.encoding = AUDIO_ENCODING_LINEAR8;
129 break;
130 case 4:
131 switch(numChannels)
133 case 1: device->Format = AL_FORMAT_MONO16; break;
134 case 2: device->Format = AL_FORMAT_STEREO16; break;
135 case 4: device->Format = AL_FORMAT_QUAD16; break;
136 case 6: device->Format = AL_FORMAT_51CHN16; break;
137 case 7: device->Format = AL_FORMAT_61CHN16; break;
138 case 8: device->Format = AL_FORMAT_71CHN16; break;
140 /* fall-through */
141 case 2:
142 info.play.precision = 16;
143 info.play.encoding = AUDIO_ENCODING_LINEAR;
144 break;
145 default:
146 AL_PRINT("Unknown format?! %x\n", device->Format);
149 frameSize = numChannels * aluBytesFromFormat(device->Format);
150 info.play.buffer_size = device->BufferSize * frameSize;
152 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
154 AL_PRINT("ioctl failed: %s\n", strerror(errno));
155 close(data->fd);
156 free(data);
157 return ALC_FALSE;
160 if(aluChannelsFromFormat(device->Format) != info.play.channels)
162 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), info.play.channels);
163 close(data->fd);
164 free(data);
165 return ALC_FALSE;
168 if(!((info.play.precision == 8 && aluBytesFromFormat(device->Format) == 1) ||
169 (info.play.precision == 16 && aluBytesFromFormat(device->Format) == 2)))
171 AL_PRINT("Could not set %d-bit output, got %d\n", aluBytesFromFormat(device->Format)*8, info.play.precision);
172 close(data->fd);
173 free(data);
174 return ALC_FALSE;
177 device->Frequency = info.play.sample_rate;
178 device->UpdateSize = info.play.buffer_size / 4;
180 data->data_size = device->UpdateSize * frameSize;
181 data->mix_data = calloc(1, data->data_size);
183 device->ExtraData = data;
184 data->thread = StartThread(SolarisProc, device);
185 if(data->thread == NULL)
187 device->ExtraData = NULL;
188 free(data->mix_data);
189 free(data);
190 return ALC_FALSE;
193 device->szDeviceName = strdup(deviceName);
194 return ALC_TRUE;
197 static void solaris_close_playback(ALCdevice *device)
199 solaris_data *data = (solaris_data*)device->ExtraData;
200 data->killNow = 1;
201 StopThread(data->thread);
203 close(data->fd);
205 free(data->mix_data);
206 free(data);
207 device->ExtraData = NULL;
210 static ALCboolean solaris_start_context(ALCdevice *device, ALCcontext *context)
212 device->Frequency = context->fFrequency;
213 return ALC_TRUE;
216 static void solaris_stop_context(ALCdevice *device, ALCcontext *context)
218 (void)device;
219 (void)context;
223 static ALCboolean solaris_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
225 (void)device;
226 (void)deviceName;
227 (void)frequency;
228 (void)format;
229 (void)SampleSize;
230 return ALC_FALSE;
233 static void solaris_close_capture(ALCdevice *device)
235 (void)device;
238 static void solaris_start_capture(ALCdevice *pDevice)
240 (void)pDevice;
243 static void solaris_stop_capture(ALCdevice *pDevice)
245 (void)pDevice;
248 static void solaris_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
250 (void)pDevice;
251 (void)pBuffer;
252 (void)lSamples;
255 static ALCuint solaris_available_samples(ALCdevice *pDevice)
257 (void)pDevice;
258 return 0;
262 BackendFuncs solaris_funcs = {
263 solaris_open_playback,
264 solaris_close_playback,
265 solaris_start_context,
266 solaris_stop_context,
267 solaris_open_capture,
268 solaris_close_capture,
269 solaris_start_capture,
270 solaris_stop_capture,
271 solaris_capture_samples,
272 solaris_available_samples
275 void alc_solaris_init(BackendFuncs *func_list)
277 *func_list = solaris_funcs;
280 void alc_solaris_deinit(void)
284 void alc_solaris_probe(ALCboolean capture)
286 if(type == DEVICE_PROBE)
287 AppendDeviceList(solaris_device);
288 else if(type == ALL_DEVICE_PROBE)
289 AppendAllDeviceList(solaris_device);