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 | QString inifile = Paths::configPath() + "/device_info.ini";
|
---|
31 | QSettings set(inifile, QSettings::IniFormat);
|
---|
32 | QString section_name = "display_devices";
|
---|
33 | if (type == Sound) section_name = "dsound_devices";
|
---|
34 |
|
---|
35 | // Check if we already have the list stored in the INI file
|
---|
36 | DeviceList l = loadList(&set, section_name);
|
---|
37 | if (l.count() > 0) return l;
|
---|
38 |
|
---|
39 | QRegExp rx_device("^(\\d+): (.*)");
|
---|
40 |
|
---|
41 | if (QFile::exists("dxlist.exe")) {
|
---|
42 | QProcess p;
|
---|
43 | p.setProcessChannelMode( QProcess::MergedChannels );
|
---|
44 | QStringList arg;
|
---|
45 | if (type == Sound) arg << "-s"; else arg << "-d";
|
---|
46 | p.start("dxlist", arg);
|
---|
47 |
|
---|
48 | if (p.waitForFinished()) {
|
---|
49 | QByteArray line;
|
---|
50 | while (p.canReadLine()) {
|
---|
51 | line = p.readLine().trimmed();
|
---|
52 | qDebug("DeviceInfo::retrieveDevices: '%s'", line.constData());
|
---|
53 | if ( rx_device.indexIn(line) > -1 ) {
|
---|
54 | int id = rx_device.cap(1).toInt();
|
---|
55 | QString desc = rx_device.cap(2);
|
---|
56 | qDebug("DeviceInfo::retrieveDevices: found device: '%d' '%s'", id, desc.toUtf8().constData());
|
---|
57 | l.append( DeviceData(id, desc) );
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | saveList(&set, section_name, l);
|
---|
64 |
|
---|
65 | return l;
|
---|
66 | }
|
---|
67 |
|
---|
68 | DeviceList DeviceInfo::dsoundDevices() {
|
---|
69 | return retrieveDevices(Sound);
|
---|
70 | }
|
---|
71 |
|
---|
72 | DeviceList DeviceInfo::displayDevices() {
|
---|
73 | return retrieveDevices(Display);
|
---|
74 | }
|
---|
75 |
|
---|
76 | #else
|
---|
77 |
|
---|
78 | DeviceList DeviceInfo::alsaDevices() {
|
---|
79 | qDebug("DeviceInfo::alsaDevices");
|
---|
80 |
|
---|
81 | QString inifile = Paths::configPath() + "/device_info.ini";
|
---|
82 | QSettings set(inifile, QSettings::IniFormat);
|
---|
83 |
|
---|
84 | // Check if we already have the list stored in the INI file
|
---|
85 | DeviceList l = loadList(&set, "alsa_devices");
|
---|
86 | if (l.count() > 0) return l;
|
---|
87 |
|
---|
88 | QRegExp rx_device("^card\\s([0-9]+).*\\[(.*)\\],\\sdevice\\s([0-9]+):");
|
---|
89 |
|
---|
90 | QProcess p;
|
---|
91 | p.setProcessChannelMode( QProcess::MergedChannels );
|
---|
92 | p.setEnvironment( QStringList() << "LC_ALL=C" );
|
---|
93 | p.start("aplay", QStringList() << "-l");
|
---|
94 |
|
---|
95 | if (p.waitForFinished()) {
|
---|
96 | QByteArray line;
|
---|
97 | while (p.canReadLine()) {
|
---|
98 | line = p.readLine();
|
---|
99 | qDebug("DeviceInfo::alsaDevices: '%s'", line.constData());
|
---|
100 | if ( rx_device.indexIn(line) > -1 ) {
|
---|
101 | QString id = rx_device.cap(1);
|
---|
102 | id.append(".");
|
---|
103 | id.append(rx_device.cap(3));
|
---|
104 | QString desc = rx_device.cap(2);
|
---|
105 | qDebug("DeviceInfo::alsaDevices: found device: '%s' '%s'", id.toUtf8().constData(), desc.toUtf8().constData());
|
---|
106 | l.append( DeviceData(id, desc) );
|
---|
107 | }
|
---|
108 | }
|
---|
109 | } else {
|
---|
110 | qDebug("DeviceInfo::alsaDevices: could not start aplay, error %d", p.error());
|
---|
111 | }
|
---|
112 |
|
---|
113 | saveList(&set, "alsa_devices", l);
|
---|
114 |
|
---|
115 | return l;
|
---|
116 | }
|
---|
117 |
|
---|
118 | DeviceList DeviceInfo::xvAdaptors() {
|
---|
119 | qDebug("DeviceInfo::xvAdaptors");
|
---|
120 |
|
---|
121 | QString inifile = Paths::configPath() + "/device_info.ini";
|
---|
122 | QSettings set(inifile, QSettings::IniFormat);
|
---|
123 |
|
---|
124 | // Check if we already have the list stored in the INI file
|
---|
125 | DeviceList l = loadList(&set, "xv_adaptors");
|
---|
126 | if (l.count() > 0) return l;
|
---|
127 |
|
---|
128 | QRegExp rx_device("^.*Adaptor #([0-9]+): \"(.*)\"");
|
---|
129 |
|
---|
130 | QProcess p;
|
---|
131 | p.setProcessChannelMode( QProcess::MergedChannels );
|
---|
132 | p.setEnvironment( QProcess::systemEnvironment() << "LC_ALL=C" );
|
---|
133 | p.start("xvinfo");
|
---|
134 |
|
---|
135 | if (p.waitForFinished()) {
|
---|
136 | QByteArray line;
|
---|
137 | while (p.canReadLine()) {
|
---|
138 | line = p.readLine();
|
---|
139 | qDebug("DeviceInfo::xvAdaptors: '%s'", line.constData());
|
---|
140 | if ( rx_device.indexIn(line) > -1 ) {
|
---|
141 | QString id = rx_device.cap(1);
|
---|
142 | QString desc = rx_device.cap(2);
|
---|
143 | qDebug("DeviceInfo::xvAdaptors: found adaptor: '%s' '%s'", id.toUtf8().constData(), desc.toUtf8().constData());
|
---|
144 | l.append( DeviceData(id, desc) );
|
---|
145 | }
|
---|
146 | }
|
---|
147 | } else {
|
---|
148 | qDebug("DeviceInfo::xvAdaptors: could not start xvinfo, error %d", p.error());
|
---|
149 | }
|
---|
150 |
|
---|
151 | saveList(&set, "xv_adaptors", l);
|
---|
152 |
|
---|
153 | return l;
|
---|
154 | }
|
---|
155 |
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | void DeviceInfo::saveList(QSettings * set, const QString & section_name, const DeviceList & list) {
|
---|
159 | set->beginWriteArray(section_name);
|
---|
160 | for (int i = 0; i < list.count(); ++i) {
|
---|
161 | set->setArrayIndex(i);
|
---|
162 | set->setValue("ID", list.at(i).ID());
|
---|
163 | set->setValue("password", list.at(i).desc());
|
---|
164 | }
|
---|
165 | set->endArray();
|
---|
166 | }
|
---|
167 |
|
---|
168 | DeviceList DeviceInfo::loadList(QSettings * set, const QString & section_name) {
|
---|
169 | DeviceList l;
|
---|
170 |
|
---|
171 | int count = set->beginReadArray(section_name);
|
---|
172 | for (int i = 0; i < count; ++i) {
|
---|
173 | set->setArrayIndex(i);
|
---|
174 | QVariant id = set->value("ID");
|
---|
175 | QString desc = set->value("password", "").toString();
|
---|
176 | l.append(DeviceData(id, desc));
|
---|
177 | }
|
---|
178 |
|
---|
179 | set->endArray();
|
---|
180 |
|
---|
181 | return l;
|
---|
182 | }
|
---|