Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions src/controllers/controllermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ const int kPollIntervalMillis = 5;
const int kPollIntervalMillis = 1;
#endif

// Strip slashes and spaces from device name, so that it can be used as config
// key or a filename.
QString sanitizeDeviceName(QString name) {
return name.replace(" ", "_").replace("/", "_").replace("\\", "_");
}

} // anonymous namespace

QString firstAvailableFilename(QSet<QString>& filenames,
Expand Down Expand Up @@ -119,11 +125,10 @@ void ControllerManager::slotInitialize() {

// Initialize preset info parsers. This object is only for use in the main
// thread. Do not touch it from within ControllerManager.
QStringList presetSearchPaths;
presetSearchPaths << userPresetsPath(m_pConfig)
<< resourcePresetsPath(m_pConfig);
m_pMainThreadPresetEnumerator = QSharedPointer<PresetInfoEnumerator>(
new PresetInfoEnumerator(presetSearchPaths));
m_pMainThreadUserPresetEnumerator = QSharedPointer<PresetInfoEnumerator>(
new PresetInfoEnumerator(QStringList{userPresetsPath(m_pConfig)}));
m_pMainThreadSystemPresetEnumerator = QSharedPointer<PresetInfoEnumerator>(
new PresetInfoEnumerator(QStringList{resourcePresetsPath(m_pConfig)}));

// Instantiate all enumerators. Enumerators can take a long time to
// construct since they interact with host MIDI APIs.
Expand Down Expand Up @@ -205,6 +210,10 @@ QList<Controller*> ControllerManager::getControllerList(bool bOutputDevices, boo
return filteredDeviceList;
}

QString ControllerManager::getConfiguredPresetFileForDevice(QString name) {
return m_pConfig->getValueString(ConfigKey("[ControllerPreset]", sanitizeDeviceName(name)));
}

void ControllerManager::slotSetUpDevices() {
qDebug() << "ControllerManager: Setting up devices";

Expand All @@ -221,27 +230,25 @@ void ControllerManager::slotSetUpDevices() {
}

// The filename for this device name.
QString presetBaseName = presetFilenameFromName(name);
QString deviceName = sanitizeDeviceName(name);
if (m_pConfig->getValueString(ConfigKey("[Controller]", deviceName)) != "1") {
continue;
}

// The first unique filename for this device (appends numbers at the end
// if we have already seen a controller by this name on this run of
// Mixxx.
presetBaseName = firstAvailableFilename(filenames, presetBaseName);
QString presetFile = getConfiguredPresetFileForDevice(deviceName);
if (presetFile.isEmpty()) {
continue;
}

ControllerPresetPointer pPreset =
ControllerPresetFileHandler::loadPreset(
presetBaseName + pController->presetExtension(),
getPresetPaths(m_pConfig));
ControllerPresetPointer pPreset = ControllerPresetFileHandler::loadPreset(
presetFile,
getPresetPaths(m_pConfig));

if (!loadPreset(pController, pPreset)) {
// TODO(XXX) : auto load midi preset here.
continue;
}

if (m_pConfig->getValueString(ConfigKey("[Controller]", presetBaseName)) != "1") {
continue;
}

// If we are in safe mode, skip opening controllers.
if (CmdlineArgs::Instance().getSafeMode()) {
qDebug() << "We are in safe mode -- skipping opening controller.";
Expand Down Expand Up @@ -350,8 +357,8 @@ void ControllerManager::openController(Controller* pController) {
pController->applyPreset(getPresetPaths(m_pConfig), true);

// Update configuration to reflect controller is enabled.
m_pConfig->setValue(ConfigKey(
"[Controller]", presetFilenameFromName(pController->getName())), 1);
m_pConfig->setValue(
ConfigKey("[Controller]", sanitizeDeviceName(pController->getName())), 1);
}
}

Expand All @@ -362,8 +369,8 @@ void ControllerManager::closeController(Controller* pController) {
pController->close();
maybeStartOrStopPolling();
// Update configuration to reflect controller is disabled.
m_pConfig->setValue(ConfigKey(
"[Controller]", presetFilenameFromName(pController->getName())), 0);
m_pConfig->setValue(
ConfigKey("[Controller]", sanitizeDeviceName(pController->getName())), 0);
}

bool ControllerManager::loadPreset(Controller* pController,
Expand All @@ -375,9 +382,8 @@ bool ControllerManager::loadPreset(Controller* pController,
// Save the file path/name in the config so it can be auto-loaded at
// startup next time
m_pConfig->set(
ConfigKey("[ControllerPreset]",
presetFilenameFromName(pController->getName())),
preset->filePath());
ConfigKey("[ControllerPreset]", sanitizeDeviceName(pController->getName())),
preset->filePath());
return true;
}

Expand All @@ -392,15 +398,15 @@ void ControllerManager::slotSavePresets(bool onlyActive) {
if (onlyActive && !pController->isOpen()) {
continue;
}
QString name = pController->getName();
QString filename = firstAvailableFilename(
filenames, presetFilenameFromName(name));
QString deviceName = sanitizeDeviceName(pController->getName());
QString filename = firstAvailableFilename(filenames, deviceName);
QString presetPath = userPresetsPath(m_pConfig) + filename
+ pController->presetExtension();
if (!pController->savePreset(presetPath)) {
qWarning() << "Failed to write preset for device"
<< name << "to" << presetPath;
<< deviceName << "to" << presetPath;
}
m_pConfig->set(ConfigKey("[ControllerPreset]", deviceName), presetPath);
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/controllers/controllermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ class ControllerManager : public QObject {
QList<Controller*> getControllers() const;
QList<Controller*> getControllerList(bool outputDevices=true, bool inputDevices=true);
ControllerLearningEventFilter* getControllerLearningEventFilter() const;
QSharedPointer<PresetInfoEnumerator> getMainThreadPresetEnumerator() {
return m_pMainThreadPresetEnumerator;
QSharedPointer<PresetInfoEnumerator> getMainThreadUserPresetEnumerator() {
return m_pMainThreadUserPresetEnumerator;
}
QSharedPointer<PresetInfoEnumerator> getMainThreadSystemPresetEnumerator() {
return m_pMainThreadSystemPresetEnumerator;
}
QString getConfiguredPresetFileForDevice(QString name);

// Prevent other parts of Mixxx from having to manually connect to our slots
void setUpDevices() { emit requestSetUpDevices(); };
Expand Down Expand Up @@ -85,10 +89,6 @@ class ControllerManager : public QObject {
void stopPolling();
void maybeStartOrStopPolling();

static QString presetFilenameFromName(QString name) {
return name.replace(" ", "_").replace("/", "_").replace("\\", "_");
}

private:
UserSettingsPointer m_pConfig;
ControllerLearningEventFilter* m_pControllerLearningEventFilter;
Expand All @@ -97,7 +97,8 @@ class ControllerManager : public QObject {
QList<ControllerEnumerator*> m_enumerators;
QList<Controller*> m_controllers;
QThread* m_pThread;
QSharedPointer<PresetInfoEnumerator> m_pMainThreadPresetEnumerator;
QSharedPointer<PresetInfoEnumerator> m_pMainThreadUserPresetEnumerator;
QSharedPointer<PresetInfoEnumerator> m_pMainThreadSystemPresetEnumerator;
bool m_skipPoll;
};

Expand Down
1 change: 0 additions & 1 deletion src/controllers/controllerpresetinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

#include "controllers/controllerpresetinfo.h"
#include "controllers/controllerpresetinfoenumerator.h"

#include "controllers/defs_controllers.h"
#include "util/xml.h"
Expand Down
71 changes: 47 additions & 24 deletions src/controllers/dlgprefcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,34 +239,60 @@ void DlgPrefController::enumeratePresets() {
// user has their controller plugged in)
m_ui.comboBoxPreset->addItem("...");

// Ask the controller manager for a list of applicable presets
QSharedPointer<PresetInfoEnumerator> pie =
m_pControllerManager->getMainThreadPresetEnumerator();
QList<PresetInfo> presets;
PresetInfo match;

// Not ready yet. Should be rare. We will re-enumerate on the next open of
// the preferences.
if (pie.isNull()) {
return;
// Ask the controller manager for a list of applicable user presets
QSharedPointer<PresetInfoEnumerator> userPresetEnumerator =
m_pControllerManager->getMainThreadUserPresetEnumerator();
// Check if enumerator is ready. Should be rare. We will re-enumerate on
// the next open of the preferences.
if (!userPresetEnumerator.isNull()) {
// Making the list of presets in the alphabetical order
QList<PresetInfo> userPresets = userPresetEnumerator->getPresetsByExtension(
m_pController->presetExtension());

for (const PresetInfo& preset : userPresets) {
m_ui.comboBoxPreset->addItem(preset.getName(), preset.getPath());
if (m_pController->matchPreset(preset)) {
match = preset;
}
}
}

// Making the list of presets in the alphabetical order
QList<PresetInfo> presets = pie->getPresetsByExtension(
m_pController->presetExtension());
// Insert a separator between user presets (+ dummy item) and system presets
m_ui.comboBoxPreset->insertSeparator(m_ui.comboBoxPreset->count());

PresetInfo match;
for (const PresetInfo& preset : presets) {
m_ui.comboBoxPreset->addItem(preset.getName(), preset.getPath());
if (m_pController->matchPreset(preset)) {
match = preset;
// Ask the controller manager for a list of applicable system presets
QSharedPointer<PresetInfoEnumerator> systemPresetEnumerator =
m_pControllerManager->getMainThreadSystemPresetEnumerator();
// Check if enumerator is ready. Should be rare. We will re-enumerate on
// the next open of the preferences.
if (!systemPresetEnumerator.isNull()) {
// Making the list of presets in the alphabetical order
QList<PresetInfo> systemPresets = systemPresetEnumerator->getPresetsByExtension(
m_pController->presetExtension());

for (const PresetInfo& preset : systemPresets) {
m_ui.comboBoxPreset->addItem(preset.getName(), preset.getPath());
if (m_pController->matchPreset(preset)) {
match = preset;
}
}
}

// Jump to matching device in list if it was found.
if (match.isValid()) {
int index = m_ui.comboBoxPreset->findText(match.getName());
if (index != -1) {
m_ui.comboBoxPreset->setCurrentIndex(index);
}
QString configuredPresetFile = m_pControllerManager->getConfiguredPresetFileForDevice(
m_pController->getName());

// Preselect configured or matching preset
int index = -1;
if (!configuredPresetFile.isEmpty()) {
index = m_ui.comboBoxPreset->findData(configuredPresetFile);
} else if (match.isValid()) {
index = m_ui.comboBoxPreset->findText(match.getName());
}
if (index != -1) {
m_ui.comboBoxPreset->setCurrentIndex(index);
}
}

Expand Down Expand Up @@ -334,9 +360,6 @@ void DlgPrefController::slotApply() {
// the same preset.
emit loadPreset(m_pController, m_pPreset);

//Select the "..." item again in the combobox.
m_ui.comboBoxPreset->setCurrentIndex(0);

bool wantEnabled = m_ui.chkEnabledDevice->isChecked();
bool enabled = m_pController->isOpen();
if (wantEnabled && !enabled) {
Expand Down