1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2016 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program 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
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "deviceinfo.h"
|
---|
20 | #include "paths.h"
|
---|
21 | #include <QProcess>
|
---|
22 | #include <QFile>
|
---|
23 | #include <QSettings>
|
---|
24 |
|
---|
25 | #ifdef Q_OS_WIN
|
---|
26 |
|
---|
27 | DeviceList DeviceInfo::retrieveDevices(DeviceType type) {
|
---|
28 | qDebug("DeviceInfo::retrieveDevices: %d", type);
|
---|
29 |
|
---|
30 | DeviceList l;
|
---|
31 |
|
---|
32 | #ifdef CACHE_DEVICE_INFO
|
---|
33 | QString inifile = Paths::configPath() + "/device_info.ini";
|
---|
34 | QSettings set(inifile, QSettings::IniFormat);
|
---|
35 | QString section_name = "display-devices";
|
---|
36 | if (type == Sound) section_name = "dsound-devices";
|
---|
37 |
|
---|
38 | // Check if we already have the list stored in the INI file
|
---|
39 | l = loadList(&set, section_name);
|
---|
40 | if (l.count() > 0) return l;
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | QRegExp rx_device("^(\\d+): (.*)");
|
---|
44 |
|
---|
45 | if (QFile::exists("dxlist.exe")) {
|
---|
46 | QProcess p;
|
---|
47 | p.setProcessChannelMode( QProcess::MergedChannels );
|
---|
48 | QStringList arg;
|
---|
49 | if (type == Sound) arg << "-s"; else arg << "-d";
|
---|
50 | p.start("dxlist", arg);
|
---|
51 |
|
---|
52 | if (p.waitForFinished()) {
|
---|
53 | QByteArray line;
|
---|
54 | while (p.canReadLine()) {
|
---|
55 | line = p.readLine().trimmed();
|
---|
56 | qDebug("DeviceInfo::retrieveDevices: '%s'", line.constData());
|
---|
57 | if ( rx_device.indexIn(line) > -1 ) {
|
---|
58 | int id = rx_device.cap(1).toInt();
|
---|
59 | QString desc = rx_device.cap(2);
|
---|
60 | qDebug("DeviceInfo::retrieveDevices: found device: '%d' '%s'", id, desc.toUtf8().constData());
|
---|
61 | l.append( DeviceData(id, desc) );
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | #ifdef CACHE_DEVICE_INFO
|
---|
68 | saveList(&set, section_name, l);
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | return l;
|
---|
72 | }
|
---|
73 |
|
---|
74 | DeviceList DeviceInfo::dsoundDevices() {
|
---|
75 | return retrieveDevices(Sound);
|
---|
76 | }
|
---|
77 |
|
---|
78 | DeviceList DeviceInfo::displayDevices() {
|
---|
79 | return retrieveDevices(Display);
|
---|
80 | }
|
---|
81 |
|
---|
82 | #else
|
---|
83 |
|
---|
84 | DeviceList DeviceInfo::alsaDevices() {
|
---|
85 | qDebug("DeviceInfo::alsaDevices");
|
---|
86 |
|
---|
87 | DeviceList l;
|
---|
88 |
|
---|
89 | #ifdef CACHE_DEVICE_INFO
|
---|
90 | QString inifile = Paths::configPath() + "/device_info.ini";
|
---|
91 | QSettings set(inifile, QSettings::IniFormat);
|
---|
92 |
|
---|
93 | // Check if we already have the list stored in the INI file
|
---|
94 | l = loadList(&set, "alsa-devices");
|
---|
95 | if (l.count() > 0) return l;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | QRegExp rx_device("^card\\s([0-9]+).*\\[(.*)\\],\\sdevice\\s([0-9]+):");
|
---|
99 |
|
---|
100 | QProcess p;
|
---|
101 | p.setProcessChannelMode( QProcess::MergedChannels );
|
---|
102 | p.setEnvironment( QStringList() << "LC_ALL=C" );
|
---|
103 | p.start("aplay", QStringList() << "-l");
|
---|
104 |
|
---|
105 | if (p.waitForFinished()) {
|
---|
106 | QByteArray line;
|
---|
107 | while (p.canReadLine()) {
|
---|
108 | line = p.readLine();
|
---|
109 | qDebug("DeviceInfo::alsaDevices: '%s'", line.constData());
|
---|
110 | if ( rx_device.indexIn(line) > -1 ) {
|
---|
111 | QString id = rx_device.cap(1);
|
---|
112 | id.append(".");
|
---|
113 | id.append(rx_device.cap(3));
|
---|
114 | QString desc = rx_device.cap(2);
|
---|
115 | qDebug("DeviceInfo::alsaDevices: found device: '%s' '%s'", id.toUtf8().constData(), desc.toUtf8().constData());
|
---|
116 | l.append( DeviceData(id, desc) );
|
---|
117 | }
|
---|
118 | }
|
---|
119 | } else {
|
---|
120 | qDebug("DeviceInfo::alsaDevices: could not start aplay, error %d", p.error());
|
---|
121 | }
|
---|
122 |
|
---|
123 | #ifdef CACHE_DEVICE_INFO
|
---|
124 | saveList(&set, "alsa-devices", l);
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | return l;
|
---|
128 | }
|
---|
129 |
|
---|
130 | DeviceList DeviceInfo::xvAdaptors() {
|
---|
131 | qDebug("DeviceInfo::xvAdaptors");
|
---|
132 |
|
---|
133 | DeviceList l;
|
---|
134 |
|
---|
135 | #ifdef CACHE_DEVICE_INFO
|
---|
136 | QString inifile = Paths::configPath() + "/device_info.ini";
|
---|
137 | QSettings set(inifile, QSettings::IniFormat);
|
---|
138 |
|
---|
139 | // Check if we already have the list stored in the INI file
|
---|
140 | l = loadList(&set, "xv-adaptors");
|
---|
141 | if (l.count() > 0) return l;
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | QRegExp rx_device("^.*Adaptor #([0-9]+): \"(.*)\"");
|
---|
145 |
|
---|
146 | QProcess p;
|
---|
147 | p.setProcessChannelMode( QProcess::MergedChannels );
|
---|
148 | p.setEnvironment( QProcess::systemEnvironment() << "LC_ALL=C" );
|
---|
149 | p.start("xvinfo");
|
---|
150 |
|
---|
151 | if (p.waitForFinished()) {
|
---|
152 | QByteArray line;
|
---|
153 | while (p.canReadLine()) {
|
---|
154 | line = p.readLine();
|
---|
155 | qDebug("DeviceInfo::xvAdaptors: '%s'", line.constData());
|
---|
156 | if ( rx_device.indexIn(line) > -1 ) {
|
---|
157 | QString id = rx_device.cap(1);
|
---|
158 | QString desc = rx_device.cap(2);
|
---|
159 | qDebug("DeviceInfo::xvAdaptors: found adaptor: '%s' '%s'", id.toUtf8().constData(), desc.toUtf8().constData());
|
---|
160 | l.append( DeviceData(id, desc) );
|
---|
161 | }
|
---|
162 | }
|
---|
163 | } else {
|
---|
164 | qDebug("DeviceInfo::xvAdaptors: could not start xvinfo, error %d", p.error());
|
---|
165 | }
|
---|
166 |
|
---|
167 | #ifdef CACHE_DEVICE_INFO
|
---|
168 | saveList(&set, "xv-adaptors", l);
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | return l;
|
---|
172 | }
|
---|
173 |
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | #ifdef CACHE_DEVICE_INFO
|
---|
177 | void DeviceInfo::saveList(QSettings * set, const QString & section_name, const DeviceList & list) {
|
---|
178 | set->beginWriteArray(section_name);
|
---|
179 | for (int i = 0; i < list.count(); ++i) {
|
---|
180 | set->setArrayIndex(i);
|
---|
181 | set->setValue("ID", list.at(i).ID());
|
---|
182 | set->setValue("description", list.at(i).desc());
|
---|
183 | }
|
---|
184 | set->endArray();
|
---|
185 | }
|
---|
186 |
|
---|
187 | DeviceList DeviceInfo::loadList(QSettings * set, const QString & section_name) {
|
---|
188 | DeviceList l;
|
---|
189 |
|
---|
190 | int count = set->beginReadArray(section_name);
|
---|
191 | for (int i = 0; i < count; ++i) {
|
---|
192 | set->setArrayIndex(i);
|
---|
193 | QVariant id = set->value("ID");
|
---|
194 | QString desc = set->value("description", "").toString();
|
---|
195 | l.append(DeviceData(id, desc));
|
---|
196 | }
|
---|
197 |
|
---|
198 | set->endArray();
|
---|
199 |
|
---|
200 | return l;
|
---|
201 | }
|
---|
202 | #endif
|
---|