Remove 'v' from the source enum names
[openal-soft.git] / Alc / backends / solaris.c
blob1c781387d2c02a262eb1fc080bf672ffe2c90e8f
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>
34 #include "alMain.h"
35 #include "alu.h"
37 #include <sys/audioio.h>
40 static const ALCchar solaris_device[] = "Solaris Default";
42 static const char *solaris_driver = "/dev/audio";
44 typedef struct {
45 int fd;
46 volatile int killNow;
47 ALvoid *thread;
49 ALubyte *mix_data;
50 int data_size;
51 } solaris_data;
54 static ALuint SolarisProc(ALvoid *ptr)
56 ALCdevice *Device = (ALCdevice*)ptr;
57 solaris_data *data = (solaris_data*)Device->ExtraData;
58 ALint frameSize;
59 int wrote;
61 SetRTPriority();
63 frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
65 while(!data->killNow && Device->Connected)
67 ALint len = data->data_size;
68 ALubyte *WritePtr = data->mix_data;
70 aluMixData(Device, 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 ERR("write failed: %s\n", strerror(errno));
79 ALCdevice_Lock(Device);
80 aluHandleDisconnect(Device);
81 ALCdevice_Unlock(Device);
82 break;
85 Sleep(1);
86 continue;
89 len -= wrote;
90 WritePtr += wrote;
94 return 0;
98 static ALCenum solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
100 solaris_data *data;
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(solaris_driver, O_WRONLY);
111 if(data->fd == -1)
113 free(data);
114 ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
115 return ALC_INVALID_VALUE;
118 device->DeviceName = 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_LINEAR8 && device->FmtType == DevFmtUByte) ||
186 (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
187 (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
188 (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
190 ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
191 info.play.precision, info.play.encoding);
192 return ALC_FALSE;
195 device->Frequency = info.play.sample_rate;
196 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
198 SetDefaultChannelOrder(device);
200 return ALC_TRUE;
203 static ALCboolean solaris_start_playback(ALCdevice *device)
205 solaris_data *data = (solaris_data*)device->ExtraData;
207 data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
208 data->mix_data = calloc(1, data->data_size);
210 data->thread = StartThread(SolarisProc, device);
211 if(data->thread == NULL)
213 free(data->mix_data);
214 data->mix_data = NULL;
215 return ALC_FALSE;
218 return ALC_TRUE;
221 static void solaris_stop_playback(ALCdevice *device)
223 solaris_data *data = (solaris_data*)device->ExtraData;
225 if(!data->thread)
226 return;
228 data->killNow = 1;
229 StopThread(data->thread);
230 data->thread = NULL;
232 data->killNow = 0;
233 if(ioctl(data->fd, AUDIO_DRAIN) < 0)
234 ERR("Error draining device: %s\n", strerror(errno));
236 free(data->mix_data);
237 data->mix_data = NULL;
241 static const BackendFuncs solaris_funcs = {
242 solaris_open_playback,
243 solaris_close_playback,
244 solaris_reset_playback,
245 solaris_start_playback,
246 solaris_stop_playback,
247 NULL,
248 NULL,
249 NULL,
250 NULL,
251 NULL,
252 NULL,
253 ALCdevice_LockDefault,
254 ALCdevice_UnlockDefault,
255 ALCdevice_GetLatencyDefault
258 ALCboolean alc_solaris_init(BackendFuncs *func_list)
260 ConfigValueStr("solaris", "device", &solaris_driver);
262 *func_list = solaris_funcs;
263 return ALC_TRUE;
266 void alc_solaris_deinit(void)
270 void alc_solaris_probe(enum DevProbe type)
272 switch(type)
274 case ALL_DEVICE_PROBE:
276 #ifdef HAVE_STAT
277 struct stat buf;
278 if(stat(solaris_driver, &buf) == 0)
279 #endif
280 AppendAllDevicesList(solaris_device);
282 break;
284 case CAPTURE_DEVICE_PROBE:
285 break;