int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setOrganizationName(APP_ORG); a.setApplicationVersion(APP_VERSION); a.setApplicationName(APP_NAME); QTranslator translator; const QStringList localeDirs({QString("%1/languages").arg(QDir::currentPath()), QString(qApp->applicationDirPath() + "/languages"), QString("/usr/share/%1/languages").arg(APP_NAME), QString("/usr/local/share/%1/languages").arg(APP_NAME), QString(QDir::home().absolutePath() + "/.local/share/%1/languages").arg(APP_NAME), QString(QDir::currentPath().left(QDir::currentPath().lastIndexOf("/")) + "/share/%1/languages").arg(APP_NAME)}); const QString langFile(qApp->applicationName()); foreach(const QString &dir, localeDirs){ if (translator.load(QLocale::system(),langFile, "_", dir )) { qApp->installTranslator(&translator); break; } } QApplication::setQuitOnLastWindowClosed(false); PopupWindow w; w.hide(); QSharedMemory sharedMemory; sharedMemory.setKey("QtAlsaVolume"); if (sharedMemory.attach()) { return 0; } if (!sharedMemory.create(1)) { return 0; } else{ return a.exec(); } }
/** * @brief Executed when new instance connect command is sent to LocalServer */ void Rshare::slotConnectionEstablished() { QLocalSocket *socket = localServer->nextPendingConnection(); socket->close(); delete socket; QSharedMemory newArgs; newArgs.setKey(QString(TARGET) + "_newArgs"); if (!newArgs.attach()) { std::cerr << "(EE) Rshare::slotConnectionEstablished() Unable to attach to shared memory segment." << newArgs.errorString().toStdString() << std::endl; return; } QBuffer buffer; QDataStream in(&buffer); QStringList args; newArgs.lock(); buffer.setData((char*)newArgs.constData(), newArgs.size()); buffer.open(QBuffer::ReadOnly); in >> args; newArgs.unlock(); newArgs.detach(); emit newArgsReceived(args); while (!args.empty()) { std::cerr << "Rshare::slotConnectionEstablished args:" << QString(args.takeFirst()).toStdString() << std::endl; } }
bool isRunning(){ if(!appShare.create(16)){ if(!appShare.attach() || !appShare.lock()) return false; void* data = appShare.data(); snprintf((char*)data, 16, "Open"); appShare.unlock(); return true; } return false; }
static mydsp *create_shared_dsp(){ QString key = "radium_crashreporter_" + QString::number(QDateTime::currentMSecsSinceEpoch()); QSharedMemory *shared = new QSharedMemory(key); if(shared->create(sizeof(mydsp))==false){ fprintf(stderr,"COMPRESSOR_create_shared: Couldn't create... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toAscii().data()); return NULL; } void *memory = shared->data(); mydsp *dsp = new(memory) mydsp(); #if 0 printf("memory: %p, shared: %p\n",memory,shared); dsp->key = key; dsp->shared = shared; #endif //printf("system: %d\n",system(QString(QString("xterm -e gdb /home/kjetil/radium_compressor/radium_compressor --args --ladspa-slave ")+key).toAscii())); printf("system: %d\n",system(QString(QString("xterm -e gdb --args /home/kjetil/radium_compressor/radium_compressor --ladspa-slave ")+key+" &").toAscii())); return dsp; #if 0 if(fork()!=0){ return dsp; }else{ QSharedMemory *shared = new QSharedMemory(key); if(shared->attach()==false){ fprintf(stderr,"Ladspa compressor: Couldn't attach... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toAscii().data()); exit(0); } char *argv[1]={(char*)"radium_compressor"}; mydsp *dsp = (mydsp*)shared->data(); fprintf(stderr,"dsp: %p.\n",dsp); portData* port_data = new portData(dsp->getNumInputs(), dsp->getNumOutputs()); dsp->buildUserInterface(port_data); // This is the second time buildUserInterface is called twice on the dsp data. One time in each process. start_program(1,argv,new PLUGIN(0, port_data, dsp)); exit(0); return NULL; } #endif }
int SingleApp(){//TODO:problem. int ret = 1; QSharedMemory sharedMemory; sharedMemory.setKey("FishProjectApp"); // if (sharedMemory.create(1) && sharedMemory.error() != QSharedMemory::AlreadyExists) if(sharedMemory.attach()) { qDebug()<<"single"; return 0; } sharedMemory.create(1); qDebug()<<"create"; return ret; }
int consumer() { QSharedMemory consumer; consumer.setKey("market"); //qDebug("consumer starting"); int tries = 0; while (!consumer.attach()) { if (tries == 5000) { qWarning() << "consumer exiting, waiting too long"; return EXIT_FAILURE; } ++tries; QTest::qSleep(1); } //qDebug("consumer attached"); int i = 0; while (true) { if (!consumer.lock()) { qWarning() << "Could not lock" << consumer.key(); return EXIT_FAILURE; } if (get(consumer, 0) == 'Q') { set(consumer, 0, ++i); //qDebug() << "consumer sets" << i; } if (get(consumer, 0) == 'E') { if (!consumer.unlock()) { qWarning() << "Could not unlock" << consumer.key(); return EXIT_FAILURE; } break; } if (!consumer.unlock()) { qWarning() << "Could not unlock" << consumer.key(); return EXIT_FAILURE; } QTest::qSleep(10); } //qDebug("consumer detaching"); if (!consumer.detach()) { qWarning() << "Could not detach" << consumer.key(); return EXIT_FAILURE; } return EXIT_SUCCESS; }
void *COMPRESSOR_create_ladspa(const char *key){ QSharedMemory *shared = new QSharedMemory(key); if(shared->attach()==false){ fprintf(stderr,"Ladspa compressor: Couldn't attach... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toAscii().data()); exit(0); } mydsp *dsp = (mydsp*)shared->data(); fprintf(stderr,"dsp: %p.\n",dsp); portData* port_data = new portData(dsp->mydsp::getNumInputs(), dsp->mydsp::getNumOutputs()); dsp->mydsp::buildUserInterface(port_data); // buildUserInterface is called twice on the dsp data. One time in each process. return new PLUGIN(0, port_data, dsp); }
static QSharedMemory *createSharedMemory(qint32 key, int byteCount) { QSharedMemory *sharedMemory = new QSharedMemory(QString(valueKeyTemplateString).arg(key)); bool sharedMemoryIsCreated = sharedMemory->create(byteCount); if (!sharedMemoryIsCreated) { if (sharedMemory->isAttached()) sharedMemory->attach(); sharedMemory->detach(); sharedMemoryIsCreated = sharedMemory->create(byteCount); } if (sharedMemoryIsCreated) { globalSharedMemoryCache.insert(key, sharedMemory); return sharedMemory; } return 0; }
void setBool(bool value, QString name) { QSharedMemory* sharedMemory = boolMemoryMap.value(name); if(sharedMemory->isAttached()) { sharedMemory->detach(); } int size = sizeof(bool); if (!sharedMemory->create(size)) { sharedMemory->attach(QSharedMemory::ReadWrite); } sharedMemory->lock(); bool *array = (bool*)sharedMemory->data(); memcpy(array, &value, qMin(sharedMemory->size(), size)); sharedMemory->unlock(); }
void setString(const char value[], QString name) { QSharedMemory* sharedMemory = stringMemoryMap.value(name); if(sharedMemory->isAttached()) sharedMemory->detach(); int size = strlen(value) + sizeof(char); if (!sharedMemory->create(size)) { if (!sharedMemory->isAttached()) { sharedMemory->attach(QSharedMemory::ReadWrite); } } sharedMemory->lock(); char *to = (char*)sharedMemory->data(); memcpy(to, value, qMin(sharedMemory->size(), size)); sharedMemory->unlock(); }
// Example how to get json using shared memory. Not tested :-D std::pair<bool, QString> json() { QSharedMemory sharedMemory; if (!sharedMemory.attach()) { return std::make_pair<bool, QString>(false, QString()); } QBuffer buffer; QDataStream in(&buffer); QString json; sharedMemory.lock(); buffer.setData((char*)sharedMemory.constData(), sharedMemory.size()); buffer.open(QBuffer::ReadOnly); in >> json; sharedMemory.unlock(); sharedMemory.detach(); return std::make_pair<bool, QString>(false, QString()); }
int getInt(QString name) { QSharedMemory* sharedMemory = intMemoryMap[name]; sharedMemory->attach(QSharedMemory::ReadOnly); int returnValue = 0; if(!sharedMemory->isAttached()) { return returnValue; } sharedMemory->lock(); int* value = (int*)sharedMemory->data(); memcpy(&returnValue, value, sizeof(value)); sharedMemory->unlock(); sharedMemory->detach(); return returnValue; }
void setInt(int value, QString name) { QSharedMemory* sharedMemory = intMemoryMap.value(name); if(sharedMemory->isAttached()) sharedMemory->detach(); int size = sizeof(int); if (!sharedMemory->create(size)) { if (!sharedMemory->isAttached()) { sharedMemory->attach(QSharedMemory::ReadWrite); } } sharedMemory->lock(); int* to = (int*)sharedMemory->data(); memcpy(to, &value, qMin(sharedMemory->size(), size)); sharedMemory->unlock(); }
// Check if the application is running. Optionally write something to its shared memory. static bool isRunning(const char* command){ // Try to create shared memory (if successful, application was not already running) if(!appShare.create(SHMEM_SIZE) || !appShare.lock()){ // Lock existing shared memory if(!appShare.attach() || !appShare.lock()) return true; bool running = false; if(appShare.size() == SHMEM_SIZE_V015){ // Old shmem - no PID listed so assume the process is running, and print the command directly to the buffer if(command){ void* data = appShare.data(); snprintf((char*)data, SHMEM_SIZE_V015, "%s", command); } running = true; } else { // New shmem. Scan the contents of the shared memory for a PID QStringList lines = QString((const char*)appShare.constData()).split("\n"); if(pidActive(lines)){ running = true; // Append the command if(command){ lines.append(QString("Option ") + command); QByteArray newMem = lines.join("\n").left(SHMEM_SIZE).toLatin1(); // Copy the NUL byte as well as the string memcpy(appShare.data(), newMem.constData(), newMem.length() + 1); } } } if(running){ appShare.unlock(); return true; } } // Not already running. Initialize the shared memory with our PID snprintf((char*)appShare.data(), appShare.size(), "PID %ld", (long)getpid()); appShare.unlock(); return false; }
char* getString(QString name) { QSharedMemory* sharedMemory = stringMemoryMap[name]; sharedMemory->attach(QSharedMemory::ReadOnly); char* strValue = (char*)""; if(!sharedMemory->isAttached()) { return strValue; } sharedMemory->lock(); char* shared = (char*)sharedMemory->data(); strValue = new char[strlen(shared) + sizeof(char)]; strcpy(strValue, shared); sharedMemory->unlock(); sharedMemory->detach(); return strValue; }
bool getBool(QString name) { QSharedMemory* sharedMemory = boolMemoryMap[name]; sharedMemory->attach(QSharedMemory::ReadOnly); bool returnValue = false; if(!sharedMemory->isAttached()) { return returnValue; } sharedMemory->lock(); bool value = *(bool*)sharedMemory->data(); returnValue = value; sharedMemory->unlock(); sharedMemory->detach(); return returnValue; }
bool Application::isRunning() { bool running = BaseApplication::isRunning(); QSharedMemory *sharedMem = new QSharedMemory(id() + QLatin1String("-shared-memory-key"), this); if (!running) { // First instance creates shared memory and store PID if (sharedMem->create(sizeof(DWORD)) && sharedMem->lock()) { *(static_cast<DWORD*>(sharedMem->data())) = ::GetCurrentProcessId(); sharedMem->unlock(); } } else { // Later instances attach to shared memory and retrieve PID if (sharedMem->attach() && sharedMem->lock()) { ::AllowSetForegroundWindow(*(static_cast<DWORD*>(sharedMem->data()))); sharedMem->unlock(); } } if (!sharedMem->isAttached()) qWarning() << "Failed to initialize shared memory: " << sharedMem->errorString(); return running; }
int producer() { QSharedMemory producer; producer.setKey("market"); int size = 1024; if (!producer.create(size)) { if (producer.error() == QSharedMemory::AlreadyExists) { if (!producer.attach()) { qWarning() << "Could not attach to" << producer.key(); return EXIT_FAILURE; } } else { qWarning() << "Could not create" << producer.key(); return EXIT_FAILURE; } } // tell parent we're ready //qDebug("producer created and attached"); puts(""); fflush(stdout); if (!producer.lock()) { qWarning() << "Could not lock" << producer.key(); return EXIT_FAILURE; } set(producer, 0, 'Q'); if (!producer.unlock()) { qWarning() << "Could not lock" << producer.key(); return EXIT_FAILURE; } int i = 0; while (i < 5) { if (!producer.lock()) { qWarning() << "Could not lock" << producer.key(); return EXIT_FAILURE; } if (get(producer, 0) == 'Q') { if (!producer.unlock()) { qWarning() << "Could not unlock" << producer.key(); return EXIT_FAILURE; } QTest::qSleep(1); continue; } //qDebug() << "producer:" << i); ++i; set(producer, 0, 'Q'); if (!producer.unlock()) { qWarning() << "Could not unlock" << producer.key(); return EXIT_FAILURE; } QTest::qSleep(1); } if (!producer.lock()) { qWarning() << "Could not lock" << producer.key(); return EXIT_FAILURE; } set(producer, 0, 'E'); if (!producer.unlock()) { qWarning() << "Could not unlock" << producer.key(); return EXIT_FAILURE; } //qDebug("producer done"); // Sleep for a bit to let all consumers exit getchar(); return EXIT_SUCCESS; }
bool attach_jt9_() {return mem_jt9.attach();}
int main(int argc, char *argv[]) { QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication a(argc, argv); auto startMinimized = false; QString uri = ""; for (auto string : QCoreApplication::arguments()) { if (string == "-m" || string == "--minimized") startMinimized = true; if (string == "-d" || string == "--debug") Console(); if (string.startsWith("ring:")) { uri = string; } } #ifdef URI_PROTOCOL QSharedMemory* shm = new QSharedMemory("RingShm"); QSystemSemaphore* sem = new QSystemSemaphore("RingSem", 0); if (not shm->create(1024)) { if (not uri.isEmpty()) { shm->attach(); shm->lock(); char *to = (char*) shm->data(); QChar *data = uri.data(); while (!data->isNull()) { memset(to, data->toLatin1(), 1); ++data; ++to; } memset(to, 0, 1); //null terminator shm->unlock(); } sem->release(); delete shm; exit(EXIT_SUCCESS); } //Client listening to shm event memset((char*)shm->data(), 0, shm->size()); ShmClient* shmClient = new ShmClient(shm, sem); #endif QTranslator qtTranslator; qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); a.installTranslator(&qtTranslator); QTranslator lrcTranslator; lrcTranslator.load("share/libringclient/translations/lrc_" + QLocale::system().name()); a.installTranslator(&lrcTranslator); QTranslator mainTranslator; mainTranslator.load("share/ring/translations/ring_client_windows_" + QLocale::system().name()); a.installTranslator(&mainTranslator); QFont font; font.setFamily("Segoe UI"); a.setFont(font); QFile file(":/stylesheet.css"); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { a.setStyleSheet(file.readAll()); file.close(); } QCoreApplication::setOrganizationName("Savoir-faire Linux"); QCoreApplication::setOrganizationDomain("ring.cx"); QCoreApplication::setApplicationName("Ring"); if (not uri.isEmpty()) { startMinimized = false; MainWindow::instance().onRingEvent(uri); } if (not startMinimized) MainWindow::instance().show(); else { MainWindow::instance().showMinimized(); MainWindow::instance().hide(); } MainWindow::instance().createThumbBar(); #ifdef URI_PROTOCOL QObject::connect(shmClient, SIGNAL(RingEvent(QString)), &MainWindow::instance(), SLOT(onRingEvent(QString))); QObject::connect(&a, &QApplication::aboutToQuit, [&a, &shmClient, &shm, &sem]() { shmClient->terminate(); delete shmClient; delete shm; delete sem; }); #endif return a.exec(); }