Only pass nano seconds to al_nssleep
[openal-soft.git] / Alc / backends / solaris.c
blob5d51d4601d97209a458a57e792f0097f8d67c8e3
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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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"
36 #include "threads.h"
37 #include "compat.h"
39 #include <sys/audioio.h>
42 static const ALCchar solaris_device[] = "Solaris Default";
44 static const char *solaris_driver = "/dev/audio";
46 typedef struct {
47 int fd;
49 ALubyte *mix_data;
50 int data_size;
52 volatile int killNow;
53 althrd_t thread;
54 } solaris_data;
57 static int SolarisProc(void *ptr)
59 ALCdevice *Device = (ALCdevice*)ptr;
60 solaris_data *data = (solaris_data*)Device->ExtraData;
61 ALint frameSize;
62 int wrote;
64 SetRTPriority();
65 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
67 frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
69 while(!data->killNow && Device->Connected)
71 ALint len = data->data_size;
72 ALubyte *WritePtr = data->mix_data;
74 aluMixData(Device, WritePtr, len/frameSize);
75 while(len > 0 && !data->killNow)
77 wrote = write(data->fd, WritePtr, len);
78 if(wrote < 0)
80 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
82 ERR("write failed: %s\n", strerror(errno));
83 ALCdevice_Lock(Device);
84 aluHandleDisconnect(Device);
85 ALCdevice_Unlock(Device);
86 break;
89 al_nssleep(1000000);
90 continue;
93 len -= wrote;
94 WritePtr += wrote;
98 return 0;
102 static ALCenum solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
104 solaris_data *data;
106 if(!deviceName)
107 deviceName = solaris_device;
108 else if(strcmp(deviceName, solaris_device) != 0)
109 return ALC_INVALID_VALUE;
111 data = (solaris_data*)calloc(1, sizeof(solaris_data));
112 data->killNow = 0;
114 data->fd = open(solaris_driver, O_WRONLY);
115 if(data->fd == -1)
117 free(data);
118 ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
119 return ALC_INVALID_VALUE;
122 al_string_copy_cstr(&device->DeviceName, deviceName);
123 device->ExtraData = data;
124 return ALC_NO_ERROR;
127 static void solaris_close_playback(ALCdevice *device)
129 solaris_data *data = (solaris_data*)device->ExtraData;
131 close(data->fd);
132 free(data);
133 device->ExtraData = NULL;
136 static ALCboolean solaris_reset_playback(ALCdevice *device)
138 solaris_data *data = (solaris_data*)device->ExtraData;
139 audio_info_t info;
140 ALuint frameSize;
141 int numChannels;
143 AUDIO_INITINFO(&info);
145 info.play.sample_rate = device->Frequency;
147 if(device->FmtChans != DevFmtMono)
148 device->FmtChans = DevFmtStereo;
149 numChannels = ChannelsFromDevFmt(device->FmtChans);
150 info.play.channels = numChannels;
152 switch(device->FmtType)
154 case DevFmtByte:
155 info.play.precision = 8;
156 info.play.encoding = AUDIO_ENCODING_LINEAR;
157 break;
158 case DevFmtUByte:
159 info.play.precision = 8;
160 info.play.encoding = AUDIO_ENCODING_LINEAR8;
161 break;
162 case DevFmtUShort:
163 case DevFmtInt:
164 case DevFmtUInt:
165 case DevFmtFloat:
166 device->FmtType = DevFmtShort;
167 /* fall-through */
168 case DevFmtShort:
169 info.play.precision = 16;
170 info.play.encoding = AUDIO_ENCODING_LINEAR;
171 break;
174 frameSize = numChannels * BytesFromDevFmt(device->FmtType);
175 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
177 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
179 ERR("ioctl failed: %s\n", strerror(errno));
180 return ALC_FALSE;
183 if(ChannelsFromDevFmt(device->FmtChans) != info.play.channels)
185 ERR("Could not set %d channels, got %d instead\n", ChannelsFromDevFmt(device->FmtChans), info.play.channels);
186 return ALC_FALSE;
189 if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
190 (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
191 (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
192 (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
194 ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
195 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 SetDefaultChannelOrder(device);
204 return ALC_TRUE;
207 static ALCboolean solaris_start_playback(ALCdevice *device)
209 solaris_data *data = (solaris_data*)device->ExtraData;
211 data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
212 data->mix_data = calloc(1, data->data_size);
214 data->killNow = 0;
215 if(althrd_create(&data->thread, SolarisProc, device) != althrd_success)
217 free(data->mix_data);
218 data->mix_data = NULL;
219 return ALC_FALSE;
222 return ALC_TRUE;
225 static void solaris_stop_playback(ALCdevice *device)
227 solaris_data *data = (solaris_data*)device->ExtraData;
228 int res;
230 if(data->killNow)
231 return;
233 data->killNow = 1;
234 althrd_join(data->thread, &res);
236 if(ioctl(data->fd, AUDIO_DRAIN) < 0)
237 ERR("Error draining device: %s\n", strerror(errno));
239 free(data->mix_data);
240 data->mix_data = NULL;
244 static const BackendFuncs solaris_funcs = {
245 solaris_open_playback,
246 solaris_close_playback,
247 solaris_reset_playback,
248 solaris_start_playback,
249 solaris_stop_playback,
250 NULL,
251 NULL,
252 NULL,
253 NULL,
254 NULL,
255 NULL,
256 ALCdevice_GetLatencyDefault
259 ALCboolean alc_solaris_init(BackendFuncs *func_list)
261 ConfigValueStr("solaris", "device", &solaris_driver);
263 *func_list = solaris_funcs;
264 return ALC_TRUE;
267 void alc_solaris_deinit(void)
271 void alc_solaris_probe(enum DevProbe type)
273 switch(type)
275 case ALL_DEVICE_PROBE:
277 #ifdef HAVE_STAT
278 struct stat buf;
279 if(stat(solaris_driver, &buf) == 0)
280 #endif
281 AppendAllDevicesList(solaris_device);
283 break;
285 case CAPTURE_DEVICE_PROBE:
286 break;