1 /*******************************************************************************
2 *                         Goggles Audio Player Library                         *
3 ********************************************************************************
4 *           Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved      *
5 *                               ---                                            *
6 * This program is free software: you can redistribute it and/or modify         *
7 * it under the terms of the GNU General Public License as published by         *
8 * the Free Software Foundation, either version 3 of the License, or            *
9 * (at your option) any later version.                                          *
10 *                                                                              *
11 * This program is distributed in the hope that it will be useful,              *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of               *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                *
14 * GNU General Public License for more details.                                 *
15 *                                                                              *
16 * You should have received a copy of the GNU General Public License            *
17 * along with this program.  If not, see http://www.gnu.org/licenses.           *
18 ********************************************************************************/
19 #include "ap_defs.h"
20 #include "ap_device.h"
21 #include "ap_utils.h"
22 
23 namespace ap {
24 
DeviceConfig()25 DeviceConfig:: DeviceConfig() {
26   }
~DeviceConfig()27 DeviceConfig::~DeviceConfig(){
28   }
29 
30 static const FXchar * const plugin_names[DeviceLast]={
31   "none",
32   "alsa",
33   "oss",
34   "pulse",
35   "jack",
36   "wav",
37   "sndio"
38   };
39 
ap_has_plugin(FXuchar device)40 static FXbool ap_has_plugin(FXuchar device) {
41 #ifdef _WIN32
42   FXString path = FXPath::directory(FXSystem::getExecFilename()) + PATHSEPSTRING + FXSystem::dllName(FXString::value("gap_%s",plugin_names[device]));
43 #else
44   FXString path = FXPath::search(ap_get_environment("GOGGLESMM_PLUGIN_PATH",AP_PLUGIN_PATH),FXSystem::dllName(FXString::value("gap_%s",plugin_names[device])));
45 #endif
46   if (FXStat::exists(path) )
47     return true;
48   return false;
49   }
50 
51 
52 
53 
AlsaConfig()54 AlsaConfig::AlsaConfig() : device("default"), flags(0) {
55   }
56 
AlsaConfig(const FXString & d,FXuint f)57 AlsaConfig::AlsaConfig(const FXString & d,FXuint f) : device(d),flags(f) {
58   }
59 
~AlsaConfig()60 AlsaConfig::~AlsaConfig(){
61   }
62 
load(FXSettings & settings)63 void AlsaConfig::load(FXSettings & settings) {
64   device=settings.readStringEntry("alsa","device",device.text());
65 
66   if (settings.readBoolEntry("alsa","use-mmap",false))
67     flags|=DeviceMMap;
68   else
69     flags&=~DeviceMMap;
70 
71   if (settings.readBoolEntry("alsa","no-resample",false))
72     flags|=DeviceNoResample;
73   else
74     flags&=~DeviceNoResample;
75   }
76 
save(FXSettings & settings) const77 void AlsaConfig::save(FXSettings & settings) const {
78   settings.writeStringEntry("alsa","device",device.text());
79   settings.writeBoolEntry("alsa","use-mmap",flags&DeviceMMap);
80   settings.writeBoolEntry("alsa","no-resample",flags&DeviceNoResample);
81   }
82 
OSSConfig()83 OSSConfig::OSSConfig() : device("/dev/dsp"), flags(0) {
84   }
85 
OSSConfig(const FXString & d)86 OSSConfig::OSSConfig(const FXString & d): device(d), flags(0) {
87   }
88 
~OSSConfig()89 OSSConfig::~OSSConfig(){
90   }
91 
load(FXSettings & settings)92 void OSSConfig::load(FXSettings & settings) {
93   device=settings.readStringEntry("oss","device",device.text());
94   }
95 
save(FXSettings & settings) const96 void OSSConfig::save(FXSettings & settings) const {
97   settings.writeStringEntry("oss","device",device.text());
98   }
99 
100 
SndioConfig()101 SndioConfig::SndioConfig() : device("default") {
102   }
103 
SndioConfig(const FXString & d)104 SndioConfig::SndioConfig(const FXString & d): device(d) {
105   }
106 
~SndioConfig()107 SndioConfig::~SndioConfig(){
108   }
109 
load(FXSettings & settings)110 void SndioConfig::load(FXSettings & settings) {
111   device=settings.readStringEntry("sndio","device",device.text());
112   }
113 
save(FXSettings & settings) const114 void SndioConfig::save(FXSettings & settings) const {
115   settings.writeStringEntry("sndio","device",device.text());
116   }
117 
118 
OutputConfig()119 OutputConfig::OutputConfig() {
120 #if defined(__linux__) && defined(HAVE_ALSA)
121   device=DeviceAlsa;
122 #elif defined(HAVE_OSS)
123   device=DeviceOSS;
124 #elif defined(HAVE_PULSE)
125   device=DevicePulse;
126 #elif defined(HAVE_JACK)
127   device=DeviceJack;
128 #elif defined(HAVE_SNDIO)
129   device=DeviceSndio;
130 #else
131   device=DeviceWav;
132 #endif
133   }
134 
135 
136 #define AP_ENABLE_PLUGIN(plugins,device) (plugins|=(1<<(device-1)))
137 
138 
devices()139 FXuint OutputConfig::devices() {
140   FXuint plugins=0;
141 #ifdef HAVE_ALSA
142   if (ap_has_plugin(DeviceAlsa))
143     AP_ENABLE_PLUGIN(plugins,DeviceAlsa);
144 #endif
145 #ifdef HAVE_OSS
146   if (ap_has_plugin(DeviceOSS))
147     AP_ENABLE_PLUGIN(plugins,DeviceOSS);
148 #endif
149 #ifdef HAVE_PULSE
150   if (ap_has_plugin(DevicePulse))
151     AP_ENABLE_PLUGIN(plugins,DevicePulse);
152 #endif
153 #ifdef HAVE_JACK
154   if (ap_has_plugin(DeviceJack))
155     AP_ENABLE_PLUGIN(plugins,DeviceJack);
156 #endif
157 #ifdef HAVE_SNDIO
158   if (ap_has_plugin(DeviceSndio))
159     AP_ENABLE_PLUGIN(plugins,DeviceSndio);
160 #endif
161   if (ap_has_plugin(DeviceWav))
162     AP_ENABLE_PLUGIN(plugins,DeviceWav);
163   return plugins;
164   }
165 
plugin() const166 FXString OutputConfig::plugin() const {
167   if (device>=DeviceAlsa && device<DeviceLast)
168     return plugin_names[device];
169   else
170     return FXString::null;
171   }
172 
load(FXSettings & settings)173 void OutputConfig::load(FXSettings & settings) {
174   FXString output=settings.readStringEntry("engine","output",plugin_names[device]);
175   for (FXint i=DeviceAlsa;i<DeviceLast;i++) {
176     if (output==plugin_names[i]){
177       device=i;
178       break;
179       }
180     }
181   alsa.load(settings);
182   oss.load(settings);
183   sndio.load(settings);
184   }
185 
save(FXSettings & settings) const186 void OutputConfig::save(FXSettings & settings) const {
187 /*
188   FXuchar major,minor;
189   ap_get_version(major,minor);
190   settings.writeIntEntry("version","major",major);
191   settings.writeIntEntry("version","minor",minor);
192 */
193 
194   if (device>=DeviceAlsa && device<DeviceLast)
195     settings.writeStringEntry("engine","output",plugin_names[device]);
196   else
197     settings.deleteEntry("engine","output");
198 
199   alsa.save(settings);
200   oss.save(settings);
201   sndio.save(settings);
202   }
203 
204 
205 }
206