Always use "OpenAL Soft" for the short device enumeration list
[openal-soft.git] / Alc / backends / solaris.c
blob02a6c9a52d931a7187050ca2bac39b640a6dcbe3
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 Default";
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 ALint frameSize;
57 int wrote;
59 SetRTPriority();
61 frameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType);
63 while(!data->killNow && pDevice->Connected)
65 ALint len = data->data_size;
66 ALubyte *WritePtr = data->mix_data;
68 aluMixData(pDevice, WritePtr, len/frameSize);
69 while(len > 0 && !data->killNow)
71 wrote = write(data->fd, WritePtr, len);
72 if(wrote < 0)
74 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
76 ERR("write failed: %s\n", strerror(errno));
77 aluHandleDisconnect(pDevice);
78 break;
81 Sleep(1);
82 continue;
85 len -= wrote;
86 WritePtr += wrote;
90 return 0;
94 static ALCenum solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
96 char driver[64];
97 solaris_data *data;
99 strncpy(driver, GetConfigValue("solaris", "device", "/dev/audio"), sizeof(driver)-1);
100 driver[sizeof(driver)-1] = 0;
102 if(!deviceName)
103 deviceName = solaris_device;
104 else if(strcmp(deviceName, solaris_device) != 0)
105 return ALC_INVALID_VALUE;
107 data = (solaris_data*)calloc(1, sizeof(solaris_data));
108 data->killNow = 0;
110 data->fd = open(driver, O_WRONLY);
111 if(data->fd == -1)
113 free(data);
114 ERR("Could not open %s: %s\n", driver, strerror(errno));
115 return ALC_INVALID_VALUE;
118 device->szDeviceName = strdup(deviceName);
119 device->ExtraData = data;
120 return ALC_NO_ERROR;
123 static void solaris_close_playback(ALCdevice *device)
125 solaris_data *data = (solaris_data*)device->ExtraData;
127 close(data->fd);
128 free(data);
129 device->ExtraData = NULL;
132 static ALCboolean solaris_reset_playback(ALCdevice *device)
134 solaris_data *data = (solaris_data*)device->ExtraData;
135 audio_info_t info;
136 ALuint frameSize;
137 int numChannels;
139 AUDIO_INITINFO(&info);
141 info.play.sample_rate = device->Frequency;
143 if(device->FmtChans != DevFmtMono)
144 device->FmtChans = DevFmtStereo;
145 numChannels = ChannelsFromDevFmt(device->FmtChans);
146 info.play.channels = numChannels;
148 switch(device->FmtType)
150 case DevFmtByte:
151 info.play.precision = 8;
152 info.play.encoding = AUDIO_ENCODING_LINEAR;
153 break;
154 case DevFmtUByte:
155 info.play.precision = 8;
156 info.play.encoding = AUDIO_ENCODING_LINEAR8;
157 break;
158 case DevFmtUShort:
159 case DevFmtInt:
160 case DevFmtUInt:
161 case DevFmtFloat:
162 device->FmtType = DevFmtShort;
163 /* fall-through */
164 case DevFmtShort:
165 info.play.precision = 16;
166 info.play.encoding = AUDIO_ENCODING_LINEAR;
167 break;
170 frameSize = numChannels * BytesFromDevFmt(device->FmtType);
171 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
173 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
175 ERR("ioctl failed: %s\n", strerror(errno));
176 return ALC_FALSE;
179 if(ChannelsFromDevFmt(device->FmtChans) != info.play.channels)
181 ERR("Could not set %d channels, got %d instead\n", ChannelsFromDevFmt(device->FmtChans), info.play.channels);
182 return ALC_FALSE;
185 if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR &&
186 device->FmtType == DevFmtByte) ||
187 (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 &&
188 device->FmtType == DevFmtUByte) ||
189 (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR &&
190 device->FmtType == DevFmtShort) ||
191 (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR &&
192 device->FmtType == DevFmtInt)))
194 ERR("Could not set %#x sample type, got %d (%#x)\n",
195 device->FmtType, info.play.precision, info.play.encoding);
196 return ALC_FALSE;
199 device->Frequency = info.play.sample_rate;
200 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
202 data->data_size = device->UpdateSize * frameSize;
203 data->mix_data = calloc(1, data->data_size);
205 SetDefaultChannelOrder(device);
207 data->thread = StartThread(SolarisProc, device);
208 if(data->thread == NULL)
210 free(data->mix_data);
211 data->mix_data = NULL;
212 return ALC_FALSE;
215 return ALC_TRUE;
218 static void solaris_stop_playback(ALCdevice *device)
220 solaris_data *data = (solaris_data*)device->ExtraData;
222 if(!data->thread)
223 return;
225 data->killNow = 1;
226 StopThread(data->thread);
227 data->thread = NULL;
229 data->killNow = 0;
230 if(ioctl(data->fd, AUDIO_DRAIN) < 0)
231 ERR("Error draining device: %s\n", strerror(errno));
233 free(data->mix_data);
234 data->mix_data = NULL;
238 static const BackendFuncs solaris_funcs = {
239 solaris_open_playback,
240 solaris_close_playback,
241 solaris_reset_playback,
242 solaris_stop_playback,
243 NULL,
244 NULL,
245 NULL,
246 NULL,
247 NULL,
248 NULL
251 ALCboolean alc_solaris_init(BackendFuncs *func_list)
253 *func_list = solaris_funcs;
254 return ALC_TRUE;
257 void alc_solaris_deinit(void)
261 void alc_solaris_probe(enum DevProbe type)
263 #ifdef HAVE_STAT
264 struct stat buf;
265 if(stat(GetConfigValue("solaris", "device", "/dev/audio"), &buf) != 0)
266 return;
267 #endif
269 switch(type)
271 case ALL_DEVICE_PROBE:
272 AppendAllDeviceList(solaris_device);
273 break;
274 case CAPTURE_DEVICE_PROBE:
275 break;