Add a Solaris playback backend
[openal-soft.git] / Alc / solaris.c
blob66f41730658de33e057ea660cf3c2b10b11bcb14
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)
61 int len = data->data_size - remaining;
63 if(len > 0)
65 SuspendContext(NULL);
66 aluMixData(pDevice->Context, data->mix_data+remaining, len, pDevice->Format);
67 ProcessContext(NULL);
70 remaining += len;
71 wrote = write(data->fd, data->mix_data, remaining);
72 if(wrote < 0)
74 AL_PRINT("write failed: %s\n", strerror(errno));
75 remaining = 0;
77 else if(wrote > 0)
79 remaining -= wrote;
80 if(remaining > 0)
81 memmove(data->mix_data, data->mix_data+wrote, remaining);
83 else
84 Sleep(1);
87 return 0;
91 static ALCboolean solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
93 audio_info_t info;
94 ALuint frameSize;
95 char driver[64];
96 int numChannels;
97 solaris_data *data;
99 strncpy(driver, GetConfigValue("solaris", "device", "/dev/audio"), sizeof(driver)-1);
100 driver[sizeof(driver)-1] = 0;
101 if(deviceName)
103 if(strcmp(deviceName, solaris_device))
104 return ALC_FALSE;
105 device->szDeviceName = solaris_device;
107 else
108 device->szDeviceName = solaris_device;
110 data = (solaris_data*)calloc(1, sizeof(solaris_data));
111 data->killNow = 0;
113 data->fd = open(driver, O_WRONLY);
114 if(data->fd == -1)
116 free(data);
117 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
118 return ALC_FALSE;
121 numChannels = aluChannelsFromFormat(device->Format);
122 AUDIO_INITINFO(&info);
123 info.play.sample_rate = device->Frequency;
124 info.play.channels = numChannels;
126 switch(aluBytesFromFormat(device->Format))
128 case 1:
129 info.play.precision = 8;
130 info.play.encoding = AUDIO_ENCODING_LINEAR8;
131 break;
132 case 2:
133 info.play.precision = 16;
134 info.play.encoding = AUDIO_ENCODING_LINEAR;
135 break;
136 default:
137 AL_PRINT("Unknown format?! %x\n", device->Format);
140 frameSize = numChannels * aluBytesFromFormat(device->Format);
141 info.play.buffer_size = device->UpdateSize * frameSize;
143 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
145 AL_PRINT("ioctl failed: %s\n", strerror(errno));
146 close(data->fd);
147 free(data);
148 return ALC_FALSE;
151 device->Frequency = info.play.sample_rate;
153 if(aluChannelsFromFormat(device->Format) != info.play.channels)
155 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), info.play.channels);
156 close(data->fd);
157 free(data);
158 return ALC_FALSE;
161 if(!((info.play.precision == 8 && aluBytesFromFormat(device->Format) == 1) ||
162 (info.play.precision == 16 && aluBytesFromFormat(device->Format) == 2)))
164 AL_PRINT("Could not set %d-bit output, got %d\n", aluBytesFromFormat(device->Format)*8, info.play.precision);
165 close(data->fd);
166 free(data);
167 return ALC_FALSE;
170 device->UpdateSize = info.play.buffer_size / 4;
172 data->data_size = device->UpdateSize * frameSize;
173 data->mix_data = calloc(1, data->data_size);
175 device->ExtraData = data;
176 data->thread = StartThread(SolarisProc, device);
177 if(data->thread == NULL)
179 device->ExtraData = NULL;
180 free(data->mix_data);
181 free(data);
182 return ALC_FALSE;
185 return ALC_TRUE;
188 static void solaris_close_playback(ALCdevice *device)
190 solaris_data *data = (solaris_data*)device->ExtraData;
191 data->killNow = 1;
192 StopThread(data->thread);
194 close(data->fd);
196 free(data->mix_data);
197 free(data);
198 device->ExtraData = NULL;
202 static ALCboolean solaris_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
204 (void)device;
205 (void)deviceName;
206 (void)frequency;
207 (void)format;
208 (void)SampleSize;
209 return ALC_FALSE;
212 static void solaris_close_capture(ALCdevice *device)
214 (void)device;
217 static void solaris_start_capture(ALCdevice *pDevice)
219 (void)pDevice;
222 static void solaris_stop_capture(ALCdevice *pDevice)
224 (void)pDevice;
227 static void solaris_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
229 (void)pDevice;
230 (void)pBuffer;
231 (void)lSamples;
234 static ALCuint solaris_available_samples(ALCdevice *pDevice)
236 (void)pDevice;
237 return 0;
241 BackendFuncs solaris_funcs = {
242 solaris_open_playback,
243 solaris_close_playback,
244 solaris_open_capture,
245 solaris_close_capture,
246 solaris_start_capture,
247 solaris_stop_capture,
248 solaris_capture_samples,
249 solaris_available_samples
252 void alc_solaris_init(BackendFuncs *func_list)
254 *func_list = solaris_funcs;
256 solaris_device = AppendDeviceList("Solaris Software");
257 AppendAllDeviceList(solaris_device);