1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Research In Motion
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 #include "mmrendererutil.h"
40 
41 #include <QDebug>
42 #include <QDir>
43 #include <QFile>
44 #include <QJsonDocument>
45 #include <QJsonObject>
46 #include <QJsonValue>
47 #include <QMutex>
48 #include <QMutex>
49 #include <QString>
50 #include <QXmlStreamReader>
51 
52 #include <mm/renderer.h>
53 
54 QT_BEGIN_NAMESPACE
55 
56 struct MmError {
57     int errorCode;
58     const char *name;
59 };
60 
61 #define MM_ERROR_ENTRY(error) { error, #error }
62 static const MmError mmErrors[] = {
63     MM_ERROR_ENTRY(MMR_ERROR_NONE),
64     MM_ERROR_ENTRY(MMR_ERROR_UNKNOWN ),
65     MM_ERROR_ENTRY(MMR_ERROR_INVALID_PARAMETER ),
66     MM_ERROR_ENTRY(MMR_ERROR_INVALID_STATE),
67     MM_ERROR_ENTRY(MMR_ERROR_UNSUPPORTED_VALUE),
68     MM_ERROR_ENTRY(MMR_ERROR_UNSUPPORTED_MEDIA_TYPE),
69     MM_ERROR_ENTRY(MMR_ERROR_MEDIA_PROTECTED),
70     MM_ERROR_ENTRY(MMR_ERROR_UNSUPPORTED_OPERATION),
71     MM_ERROR_ENTRY(MMR_ERROR_READ),
72     MM_ERROR_ENTRY(MMR_ERROR_WRITE),
73     MM_ERROR_ENTRY(MMR_ERROR_MEDIA_UNAVAILABLE),
74     MM_ERROR_ENTRY(MMR_ERROR_MEDIA_CORRUPTED),
75     MM_ERROR_ENTRY(MMR_ERROR_OUTPUT_UNAVAILABLE),
76     MM_ERROR_ENTRY(MMR_ERROR_NO_MEMORY),
77     MM_ERROR_ENTRY(MMR_ERROR_RESOURCE_UNAVAILABLE),
78     MM_ERROR_ENTRY(MMR_ERROR_MEDIA_DRM_NO_RIGHTS),
79     MM_ERROR_ENTRY(MMR_ERROR_DRM_CORRUPTED_DATA_STORE),
80     MM_ERROR_ENTRY(MMR_ERROR_DRM_OUTPUT_PROTECTION),
81     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_HDMI),
82     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_DISPLAYPORT),
83     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_DVI),
84     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_ANALOG_VIDEO),
85     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_ANALOG_AUDIO),
86     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_TOSLINK),
87     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_SPDIF),
88     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_BLUETOOTH),
89     MM_ERROR_ENTRY(MMR_ERROR_DRM_OPL_WIRELESSHD),
90 };
91 static const unsigned int numMmErrors = sizeof(mmErrors) / sizeof(MmError);
92 
93 static QBasicMutex roleMapMutex;
94 static bool roleMapInitialized = false;
95 static QString roleMap[QAudio::CustomRole + 1];
96 
97 template <typename T, size_t N>
countof(T (&)[N])98 constexpr size_t countof(T (&)[N])
99 {
100     return N;
101 }
102 
inBounds(QAudio::Role r)103 constexpr bool inBounds(QAudio::Role r)
104 {
105     return r >= 0 && r < countof(roleMap);
106 }
107 
keyValueMapsLocation()108 QString keyValueMapsLocation()
109 {
110     QByteArray qtKeyValueMaps = qgetenv("QT_KEY_VALUE_MAPS");
111     if (qtKeyValueMaps.isNull())
112         return QStringLiteral("/etc/qt/keyvaluemaps");
113     else
114         return qtKeyValueMaps;
115 }
116 
loadMapObject(const QString & keyValueMapPath)117 QJsonObject loadMapObject(const QString &keyValueMapPath)
118 {
119     QFile mapFile(keyValueMapsLocation() + keyValueMapPath);
120     if (mapFile.open(QIODevice::ReadOnly)) {
121         QByteArray mapFileContents = mapFile.readAll();
122         QJsonDocument mapDocument = QJsonDocument::fromJson(mapFileContents);
123         if (mapDocument.isObject()) {
124             QJsonObject mapObject = mapDocument.object();
125             return mapObject;
126         }
127     }
128     return QJsonObject();
129 }
130 
loadRoleMap()131 static void loadRoleMap()
132 {
133     QMutexLocker locker(&roleMapMutex);
134 
135     if (!roleMapInitialized) {
136         QJsonObject mapObject = loadMapObject("/QAudio/Role.json");
137         if (!mapObject.isEmpty()) {
138             // Wrapping the loads in a switch like this ensures that anyone adding
139             // a new enumerator will be notified that this code must be updated. A
140             // compile error will occur because the enumerator is missing from the
141             // switch.  A compile error will also occur if the enumerator used to
142             // size the mapping table isn't updated when a new enumerator is added.
143             // One or more enumerators will be outside the bounds of the array when
144             // the wrong enumerator is used to size the array.
145             //
146             // The code loads a mapping for each enumerator because role is set
147             // to UnknownRole and all the cases drop through to the next case.
148 #pragma GCC diagnostic push
149 #pragma GCC diagnostic error "-Wswitch"
150 #define loadRoleMapping(r)                                                             \
151     case QAudio::r:                                                                    \
152         static_assert(inBounds(QAudio::r), #r " out-of-bounds."                        \
153             "  Do you need to change the enumerator used to size the mapping table"    \
154             " because you added new QAudio::Role enumerators?");                       \
155             roleMap[QAudio::r] = mapObject.value(QLatin1String(#r)).toString();
156 
157             QAudio::Role role = QAudio::UnknownRole;
158             switch (role) {
159                 loadRoleMapping(UnknownRole);
160                 loadRoleMapping(MusicRole);
161                 loadRoleMapping(VideoRole);
162                 loadRoleMapping(VoiceCommunicationRole);
163                 loadRoleMapping(AlarmRole);
164                 loadRoleMapping(NotificationRole);
165                 loadRoleMapping(RingtoneRole);
166                 loadRoleMapping(AccessibilityRole);
167                 loadRoleMapping(SonificationRole);
168                 loadRoleMapping(GameRole);
169                 loadRoleMapping(CustomRole);
170             }
171 #undef loadRoleMapping
172 #pragma GCC diagnostic pop
173 
174             if (!roleMap[QAudio::CustomRole].isEmpty()) {
175                 qWarning("CustomRole mapping ignored");
176                 roleMap[QAudio::CustomRole].clear();
177             }
178         }
179 
180         roleMapInitialized = true;
181     }
182 }
183 
mmErrorMessage(const QString & msg,mmr_context_t * context,int * errorCode)184 QString mmErrorMessage(const QString &msg, mmr_context_t *context, int *errorCode)
185 {
186     const mmr_error_info_t * const mmError = mmr_error_info(context);
187 
188     if (errorCode)
189         *errorCode = mmError->error_code;
190 
191     if (mmError->error_code < numMmErrors) {
192         return QString("%1: %2 (code %3)").arg(msg).arg(mmErrors[mmError->error_code].name)
193                                           .arg(mmError->error_code);
194     } else {
195         return QString("%1: Unknown error code %2").arg(msg).arg(mmError->error_code);
196     }
197 }
198 
checkForDrmPermission()199 bool checkForDrmPermission()
200 {
201     QDir sandboxDir = QDir::home(); // always returns 'data' directory
202     sandboxDir.cdUp(); // change to app sandbox directory
203 
204     QFile file(sandboxDir.filePath("app/native/bar-descriptor.xml"));
205     if (!file.open(QIODevice::ReadOnly)) {
206         qWarning() << "checkForDrmPermission: Unable to open bar-descriptor.xml";
207         return false;
208     }
209 
210     QXmlStreamReader reader(&file);
211     while (!reader.atEnd()) {
212         reader.readNextStartElement();
213         if (reader.name() == QLatin1String("action")
214             || reader.name() == QLatin1String("permission")) {
215             if (reader.readElementText().trimmed() == QLatin1String("access_protected_media"))
216                 return true;
217         }
218     }
219 
220     return false;
221 }
222 
qnxAudioType(QAudio::Role role)223 QString qnxAudioType(QAudio::Role role)
224 {
225     loadRoleMap();
226 
227     if (role >= 0 && role < countof(roleMap))
228         return roleMap[role];
229     else
230         return QString();
231 }
232 
qnxSupportedAudioRoles()233 QList<QAudio::Role> qnxSupportedAudioRoles()
234 {
235     loadRoleMap();
236 
237     QList<QAudio::Role> result;
238     for (size_t i = 0; i < countof(roleMap); ++i) {
239         if (!roleMap[i].isEmpty() || (i == QAudio::UnknownRole))
240             result.append(static_cast<QAudio::Role>(i));
241     }
242 
243     return result;
244 }
245 
246 QT_END_NAMESPACE
247