From de44beeae3f89889b1d07ba5d897fad009122cf2 Mon Sep 17 00:00:00 2001 From: juliagoda Date: Mon, 29 Apr 2019 20:48:25 +0200 Subject: [PATCH] Fixes and debug - #64 --- src/autoprofilewatcher.cpp | 20 +++++++--- src/inputdaemon.cpp | 9 ++++- src/joytabwidget.cpp | 9 +++-- src/localantimicroserver.cpp | 55 +++++++++++++++++++------- src/main.cpp | 65 +++++++++++++++++++++---------- src/mainsettingsdialog.cpp | 1 - src/mainwindow.cpp | 33 +++++++++++++++- src/x11extras.cpp | 75 ++++++++++++++++++++++++++++++++++-- 8 files changed, 216 insertions(+), 51 deletions(-) diff --git a/src/autoprofilewatcher.cpp b/src/autoprofilewatcher.cpp index 8d57fda5..a3e7f210 100755 --- a/src/autoprofilewatcher.cpp +++ b/src/autoprofilewatcher.cpp @@ -92,10 +92,12 @@ void AutoProfileWatcher::stopTimer() checkWindowTimer.stop(); } +// TROP void AutoProfileWatcher::runAppCheck() { qInstallMessageHandler(MessageHandler::myMessageOutput); + // TROP #ifndef QT_DEBUG_NO_OUTPUT qDebug() << qApp->applicationFilePath(); #endif @@ -111,6 +113,7 @@ void AutoProfileWatcher::runAppCheck() if (!getAppProfileAssignments().isEmpty()) { appLocation = findAppLocation(); + qDebug() << "appLocation is " << appLocation << endl; } #else // In Windows, get program location no matter what. @@ -124,6 +127,7 @@ void AutoProfileWatcher::runAppCheck() // More portable check for whether antimicro is the current application // with focus. QWidget *focusedWidget = qApp->activeWindow(); + if (focusedWidget != nullptr) qDebug() << "get active window of app" << endl; QString nowWindow = QString(); QString nowWindowClass = QString(); QString nowWindowName = QString(); @@ -133,21 +137,23 @@ void AutoProfileWatcher::runAppCheck() #elif defined(Q_OS_UNIX) long currentWindow = X11Extras::getInstance()->getWindowInFocus(); + qDebug() << "getWindowInFocus: " << currentWindow << endl; if (currentWindow > 0) { long tempWindow = X11Extras::getInstance()->findParentClient(currentWindow); + qDebug() << "findParentClient: " << tempWindow << endl; if (tempWindow > 0) currentWindow = tempWindow; nowWindow = QString::number(currentWindow); - nowWindowClass = X11Extras::getInstance()->getWindowClass(static_cast(currentWindow)); - nowWindowName = X11Extras::getInstance()->getWindowTitle(static_cast(currentWindow)); + qDebug() << "number of window now: " << nowWindow << endl; - #ifndef QT_DEBUG_NO_OUTPUT - qDebug() << nowWindowClass; - qDebug() << nowWindowName; - #endif + nowWindowClass = X11Extras::getInstance()->getWindowClass(static_cast(currentWindow)); + qDebug() << "class of window now: " << nowWindowClass << endl; + + nowWindowName = X11Extras::getInstance()->getWindowTitle(static_cast(currentWindow)); + qDebug() << "title of window now: " << nowWindowName << endl; } #endif @@ -157,6 +163,8 @@ void AutoProfileWatcher::runAppCheck() bool checkForTitleChange = getWindowNameProfileAssignments().size() > 0; + qDebug() << "window profile assignments size: " << getWindowNameProfileAssignments().size() << endl; + qDebug() << "checkForTitleChange: " << checkForTitleChange; #ifdef Q_OS_WIN diff --git a/src/inputdaemon.cpp b/src/inputdaemon.cpp index 147f0a43..1f83d34b 100755 --- a/src/inputdaemon.cpp +++ b/src/inputdaemon.cpp @@ -337,7 +337,8 @@ void InputDaemon::refresh() QEventLoop q; connect(eventWorker, &SDLEventReader::sdlStarted, &q, &QEventLoop::quit); - QMetaObject::invokeMethod(eventWorker, "refresh", Qt::BlockingQueuedConnection); + QTimer::singleShot(0, eventWorker, SLOT(refresh())); + //QMetaObject::invokeMethod(eventWorker, "refresh", Qt::BlockingQueuedConnection); if (eventWorker->isSDLOpen()) q.exec(); @@ -849,6 +850,8 @@ void InputDaemon::firstInputPass(QQueue *sdlEventQueue) } case SDL_JOYDEVICEREMOVED: case SDL_JOYDEVICEADDED: + case SDL_CONTROLLERDEVICEADDED: + case SDL_CONTROLLERDEVICEREMOVED: { sdlEventQueue->append(event); break; @@ -987,6 +990,8 @@ void InputDaemon::modifyUnplugEvents(QQueue *sdlEventQueue) } case SDL_JOYDEVICEREMOVED: case SDL_JOYDEVICEADDED: + case SDL_CONTROLLERDEVICEREMOVED: + case SDL_CONTROLLERDEVICEADDED: { tempQueue.enqueue(event); break; @@ -1163,6 +1168,7 @@ void InputDaemon::secondInputPass(QQueue *sdlEventQueue) } case SDL_JOYDEVICEREMOVED: + case SDL_CONTROLLERDEVICEREMOVED: { InputDevice *device = m_joysticks->value(event.jdevice.which); @@ -1179,6 +1185,7 @@ void InputDaemon::secondInputPass(QQueue *sdlEventQueue) } case SDL_JOYDEVICEADDED: + case SDL_CONTROLLERDEVICEADDED: { addInputDevice(event.jdevice.which); break; diff --git a/src/joytabwidget.cpp b/src/joytabwidget.cpp index 27b2d050..ba2cc66d 100755 --- a/src/joytabwidget.cpp +++ b/src/joytabwidget.cpp @@ -925,8 +925,9 @@ void JoyTabWidget::changeJoyConfig(int index) removeCurrentButtons(); emit forceTabUnflash(this); - QMetaObject::invokeMethod(&tabHelper, "readConfigFile", Qt::BlockingQueuedConnection, - Q_ARG(QString, filename)); + tabHelper.readConfigFile(filename); + // QMetaObject::invokeMethod(&tabHelper, "readConfigFile", Qt::BlockingQueuedConnection, + // Q_ARG(QString, filename)); fillButtons(); refreshSetButtons(); @@ -1539,9 +1540,9 @@ void JoyTabWidget::loadDeviceSettings() { qInstallMessageHandler(MessageHandler::myMessageOutput); - //settings.beginGroup("Controllers"); + //m_settings->beginGroup("Controllers"); loadSettings(); - //settings.endGroup(); + //m_settings->endGroup(); } bool JoyTabWidget::isDisplayingNames() diff --git a/src/localantimicroserver.cpp b/src/localantimicroserver.cpp index 51f6d190..71a92072 100755 --- a/src/localantimicroserver.cpp +++ b/src/localantimicroserver.cpp @@ -25,6 +25,7 @@ #include #include + LocalAntiMicroServer::LocalAntiMicroServer(QObject *parent) : QObject(parent) { @@ -37,18 +38,31 @@ void LocalAntiMicroServer::startLocalServer() { qInstallMessageHandler(MessageHandler::myMessageOutput); - QLocalServer::removeServer(PadderCommon::localSocketKey); - localServer->setMaxPendingConnections(1); - if (!localServer->listen(PadderCommon::localSocketKey)) + if (localServer != nullptr) { - QTextStream errorstream(stderr); - QString message("Could not start signal server. Profiles cannot be reloaded\n"); - message.append("from command-line"); - errorstream << trUtf8(message.toStdString().c_str()) << endl; + if (!localServer->hasPendingConnections()) QLocalServer::removeServer(PadderCommon::localSocketKey); + + if (localServer->maxPendingConnections() != 1) localServer->setMaxPendingConnections(1); + + if (!localServer->isListening()) + { + if (!localServer->listen(PadderCommon::localSocketKey)) + { + QTextStream errorstream(stderr); + QString message("Could not start signal server. Profiles cannot be reloaded\n"); + message.append("from command-line"); + errorstream << trUtf8(message.toStdString().c_str()) << endl; + qDebug() << "Could not start signal server. Profiles cannot be reloaded\n" << " \nfrom command-line\n " << trUtf8(message.toStdString().c_str()) << endl; + } + else + { + connect(localServer, &QLocalServer::newConnection, this, &LocalAntiMicroServer::handleOutsideConnection); + } + } } else { - connect(localServer, &QLocalServer::newConnection, this, &LocalAntiMicroServer::handleOutsideConnection); + qDebug() << "LocalAntiMicroServer::startLocalServer(): localServer is nullptr" << endl; } } @@ -56,11 +70,24 @@ void LocalAntiMicroServer::handleOutsideConnection() { qInstallMessageHandler(MessageHandler::myMessageOutput); - QLocalSocket *socket = localServer->nextPendingConnection(); - if (socket != nullptr) + if (localServer != nullptr) { - connect(socket, &QLocalSocket::disconnected, this, &LocalAntiMicroServer::handleSocketDisconnect); - connect(socket, &QLocalSocket::disconnected, socket, &QLocalSocket::deleteLater); + QLocalSocket *socket = localServer->nextPendingConnection(); + + if (socket != nullptr) + { + qDebug() << "There is next pending connection: " << socket->socketDescriptor() << endl; + connect(socket, &QLocalSocket::disconnected, this, &LocalAntiMicroServer::handleSocketDisconnect); + connect(socket, &QLocalSocket::disconnected, socket, &QLocalSocket::deleteLater); + } + else + { + qDebug() << "There isn't next pending connection: " << endl; + } + } + else + { + qDebug() << "LocalAntiMicroServer::handleOutsideConnection(): localServer is nullptr" << endl; } } @@ -78,7 +105,7 @@ void LocalAntiMicroServer::close() localServer->close(); } -QLocalServer* LocalAntiMicroServer::getLocalServer() const { - +QLocalServer* LocalAntiMicroServer::getLocalServer() const +{ return localServer; } diff --git a/src/main.cpp b/src/main.cpp index ea747ee1..845d787c 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,6 +50,7 @@ #include #include + #ifdef Q_OS_UNIX #include #include @@ -57,9 +58,9 @@ #include #include - #ifdef WITH_X11 - #include "x11extras.h" - #endif + #ifdef WITH_X11 + #include "x11extras.h" + #endif #endif @@ -81,7 +82,7 @@ static void termSignalTermHandler(int signal) { qInstallMessageHandler(MessageHandler::myMessageOutput); - Q_UNUSED(signal); + Q_UNUSED(signal) qApp->exit(0); } @@ -90,7 +91,7 @@ static void termSignalIntHandler(int signal) { qInstallMessageHandler(MessageHandler::myMessageOutput); - Q_UNUSED(signal); + Q_UNUSED(signal) qApp->exit(0); } @@ -233,11 +234,10 @@ int main(int argc, char *argv[]) appLogger.setLogLevel(cmdutility.getCurrentLogLevel()); } - if( !cmdutility.getCurrentLogFile().isEmpty() ) { - + if( !cmdutility.getCurrentLogFile().isEmpty() ) + { appLogger.setCurrentLogFile( cmdutility.getCurrentLogFile() ); appLogger.setCurrentErrorStream(nullptr); - } Q_INIT_RESOURCE(resources); @@ -253,6 +253,7 @@ int main(int argc, char *argv[]) #endif QDir configDir(PadderCommon::configPath()); + if (!configDir.exists()) { configDir.mkpath(PadderCommon::configPath()); @@ -266,8 +267,29 @@ int main(int argc, char *argv[]) // In the future, there might be a reason to actually send // messages to the QLocalServer. QLocalSocket socket; - socket.connectToServer(PadderCommon::localSocketKey); - socket.waitForConnected(1000); + + + if ((socket.state() == QLocalSocket::ConnectedState) && !socket.serverName().isEmpty()) socket.abort(); + else + { + socket.connectToServer(PadderCommon::localSocketKey); + + if (!socket.waitForConnected(3000)) + { + qDebug() << "Socket's state: " << socket.state() << endl; + qDebug() << "Server name: " << socket.serverName() << endl; + qDebug() << "Socket descriptor: " << socket.socketDescriptor() << endl; + qDebug() << "The connection hasn't been established: \nerror text -> " << socket.error() << "\nerror text 2 ->" << socket.errorString() << endl; + } + } + + if (!socket.isValid()) + { + qDebug() << "Socket is not valid" << endl; + qDebug() << "Socket's state: " << socket.state() << endl; + qDebug() << "Server name: " << socket.serverName() << endl; + qDebug() << "Socket descriptor: " << socket.socketDescriptor() << endl; + } if (socket.state() == QLocalSocket::ConnectedState) { @@ -277,14 +299,14 @@ int main(int argc, char *argv[]) // Update log info based on config values if( cmdutility.getCurrentLogLevel() == Logger::LOG_NONE && - settings.contains("LogLevel")) { - + settings.contains("LogLevel")) + { appLogger.setLogLevel( static_cast(settings.value("LogLevel").toInt()) ); } if( cmdutility.getCurrentLogFile().isEmpty() && - settings.contains("LogFile")) { - + settings.contains("LogFile")) + { appLogger.setCurrentLogFile( settings.value("LogFile").toString() ); appLogger.setCurrentErrorStream(nullptr); } @@ -312,6 +334,7 @@ int main(int argc, char *argv[]) settings.sync(); socket.disconnectFromServer(); + if (socket.waitForDisconnected(2000)) qDebug() << "Socket " << socket.socketDescriptor() << " disconnected!" << endl; deleteInputDevices(joysticks); delete joysticks; @@ -454,6 +477,7 @@ int main(int argc, char *argv[]) localServer = new LocalAntiMicroServer(); localServer->startLocalServer(); + #ifdef WITH_X11 if (QApplication::platformName() == QStringLiteral("xcb")) @@ -461,6 +485,7 @@ int main(int argc, char *argv[]) if (!cmdutility.getDisplayString().isEmpty()) { X11Extras::getInstance()->syncDisplay(cmdutility.getDisplayString()); + if (X11Extras::getInstance()->display() == nullptr) { appLogger.LogError(QObject::trUtf8("Display string \"%1\" is not valid.") @@ -506,6 +531,7 @@ int main(int argc, char *argv[]) QStringList themePathsTries = QStringList(); QList::const_iterator i; + for (i = appDirsLocations.constBegin(); i != appDirsLocations.constEnd(); ++i) { themePathsTries.append(QString("%1%2").arg(*i).arg("/icons")); qDebug() << QString("%1%2").arg(*i).arg("/icons"); @@ -528,19 +554,20 @@ int main(int argc, char *argv[]) // Update log info based on config values if( cmdutility.getCurrentLogLevel() == Logger::LOG_NONE && - settings->contains("LogLevel")) { - + settings->contains("LogLevel")) + { appLogger.setLogLevel( static_cast(settings->value("LogLevel").toInt()) ); } if( cmdutility.getCurrentLogFile().isEmpty() && - settings->contains("LogFile")) { - + settings->contains("LogFile")) + { appLogger.setCurrentLogFile( settings->value("LogFile").toString() ); appLogger.setCurrentErrorStream(nullptr); } QString targetLang = QLocale::system().name(); + if (settings->contains("Language")) { targetLang = settings->value("Language").toString(); @@ -659,10 +686,8 @@ int main(int argc, char *argv[]) inputEventThread->start(QThread::HighPriority); - int app_result = antimicro.exec(); - appLogger.Log(); // Log any remaining messages if they exist. inputEventThread->quit(); diff --git a/src/mainsettingsdialog.cpp b/src/mainsettingsdialog.cpp index eeaf93ad..e19a9735 100755 --- a/src/mainsettingsdialog.cpp +++ b/src/mainsettingsdialog.cpp @@ -434,7 +434,6 @@ void MainSettingsDialog::fillControllerMappingsTable() } else { - // TU! QString mappingString = settings->value(tempkey, QString()).toString(); if (!mappingString.isEmpty()) { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index aed16787..f6388d7c 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -66,7 +66,6 @@ #include #include #include -#include #include #include #include @@ -104,6 +103,7 @@ MainWindow::MainWindow(QMap *joysticks, ui->actionStick_Pad_Assign->setVisible(false); + // TROP - błąd wywołuje się przy autoprofile #ifdef Q_OS_UNIX #if defined(USE_SDL_2) && defined(WITH_X11) if (QApplication::platformName() == QStringLiteral("xcb")) @@ -547,11 +547,15 @@ void MainWindow::populateTrayIcon() int joystickCount = m_joysticks->size(); + qDebug() << "joystickCount: " << joystickCount << endl; + if (joystickCount > 0) { QMapIterator iter(*m_joysticks); bool useSingleList = m_settings->value("TrayProfileList", false).toBool(); + qDebug() << "TrayProfileList: " << useSingleList << endl; + if (!useSingleList && (joystickCount == 1)) useSingleList = true; int i = 0; @@ -565,6 +569,8 @@ void MainWindow::populateTrayIcon() joytabName.append(" ") .append(trUtf8("(%1)") .arg(current->getName())); + + qDebug() << "joytabName" << i << ": " << joytabName << endl; QMenu *joysticksubMenu = nullptr; if (!useSingleList) joysticksubMenu = trayIconMenu->addMenu(joytabName); @@ -596,17 +602,32 @@ void MainWindow::populateTrayIcon() newaction->setChecked(false); QString identifier = current->getStringIdentifier(); + qDebug() << "current identifier: " << current->getStringIdentifier() << endl; widget->convToUniqueIDControllerGroupSett(m_settings, QString("Controller%1LastSelected").arg(current->getGUIDString()), QString("Controller%1LastSelected").arg(current->getUniqueIDString())); QString controlEntryLastSelected = QString("Controller%1LastSelected").arg(identifier); - QFileInfo fileInfo(m_settings->value(controlEntryLastSelected).toString()); + qDebug() << "controlEntryLastSelected: " << controlEntryLastSelected << endl; + + QString contrFile = m_settings->value(controlEntryLastSelected).toString(); + + QFileInfo fileInfo(contrFile); + + qDebug() << "controlEntryLastSelected in config file: " << contrFile << endl; + + qDebug() << "fileInfo.exists(): " << fileInfo.exists() << endl; + qDebug() << "fileInfo.size(): " << fileInfo.size() << endl; + qDebug() << "fileInfo.permissions(): " << fileInfo.permissions() << endl; + if ((configIter.value() == fileInfo.baseName()) || (configIter.value() == widget->getCurrentConfigName())) { + qDebug() << "fileInfo.baseName(): " << fileInfo.baseName() << endl; + qDebug() << "widget->getCurrentConfigName(): " << widget->getCurrentConfigName() << endl; newaction->setChecked(true); } QHash tempmap; + qDebug() << "insert " << QString::number(i) << ": " << configIter.key() << endl; tempmap.insert(QString::number(i), QVariant (configIter.key())); QVariant tempvar (tempmap); newaction->setData(tempvar); @@ -617,10 +638,12 @@ void MainWindow::populateTrayIcon() if (useSingleList) { + qDebug() << "useSingleList" << endl; tempProfileList.append(newaction); } else { + qDebug() << "doesn't useSingleList" << endl; joysticksubMenu->addAction(newaction); } } @@ -632,10 +655,12 @@ void MainWindow::populateTrayIcon() if (joysticksubMenu != nullptr) { + qDebug() << "joysticksubmenu exists" << endl; newaction = new QAction(trUtf8("Open File"), joysticksubMenu); } else { + qDebug() << "created action open file for tray" << endl; newaction = new QAction(trUtf8("Open File"), trayIconMenu); } @@ -646,6 +671,7 @@ void MainWindow::populateTrayIcon() if (useSingleList) { + qDebug() << "usesinglelist" << endl; QAction *titleAction = new QAction(joytabName, trayIconMenu); titleAction->setCheckable(false); @@ -658,6 +684,7 @@ void MainWindow::populateTrayIcon() trayIconMenu->addAction(newaction); profileActions.insert(i, tempProfileList); + qDebug() << "inserted profile action " << i << ": " << tempProfileList << endl; if (iter.hasNext()) { @@ -692,6 +719,8 @@ void MainWindow::populateTrayIcon() QIcon icon = QIcon::fromTheme("antimicro", QIcon(":/images/antimicro_trayicon.png")); trayIcon->setIcon(icon); trayIcon->setContextMenu(trayIconMenu); + + qDebug() << "end of MainWindow::populateTrayIcon function" << endl; } void MainWindow::quitProgram() diff --git a/src/x11extras.cpp b/src/x11extras.cpp index 45d344e3..4eff4e11 100755 --- a/src/x11extras.cpp +++ b/src/x11extras.cpp @@ -235,14 +235,75 @@ Window X11Extras::findParentClient(Window window) Window parent = 0; Window root = 0; - Window *children = nullptr; + Window *children = 0; unsigned int num_children = 0; Window finalwindow = 0; Display *display = this->display(); - checkPropertyOnWin(windowIsViewable(display, window) && isWindowRelevant(display, window), window, parent, finalwindow, root, children, display, num_children); + if (windowIsViewable(display, window) && + isWindowRelevant(display, window)) + { + finalwindow = window; + } + else + { + bool quitTraversal = false; + + while (!quitTraversal) + { + children = 0; + + if (XQueryTree(display, window, &root, &parent, &children, &num_children)) + { + if (children) + { + // must test for NULL + XFree(children); + } + + if (parent) + { + if (windowIsViewable(display, parent) && + isWindowRelevant(display, parent)) + { + quitTraversal = true; + finalwindow = parent; + } + else if (parent == 0) + { + quitTraversal = true; + } + else if (parent == root) + { + quitTraversal = true; + } + else + { + window = parent; + } + } + else + { + quitTraversal = true; + } + } + else + { + quitTraversal = true; + } + } + } + + if (display != nullptr) qDebug() << "display in X11Extras::findParentClient(Window window) exists" << endl; + else qDebug() << "display in X11Extras::findParentClient(Window window) doesn't exist" << endl; + + qDebug() << "parent: " << parent << endl; + qDebug() << "root: " << root << endl; + qDebug() << "num_children: " << num_children << endl; + qDebug() << "finalwindow: " << finalwindow << endl; return finalwindow; + } /** @@ -622,10 +683,18 @@ unsigned long X11Extras::getWindowInFocus() int focusState = 0; Display *display = this->display(); + if (display != nullptr) qDebug() << "display in X11Extras::getWindowInFocus() exists" << endl; + else qDebug() << "display in X11Extras::getWindowInFocus() doesn't exist" << endl; + XGetInputFocus(display, ¤tWindow, &focusState); if (currentWindow > 0) - result = static_cast(currentWindow); + { + result = static_cast(currentWindow); + } + + qDebug() << "focusState of currentWindow: " << focusState << endl; + qDebug() << "result of currentWindow: " << result << endl; return result; }