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; }
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(); }
bool unlock_jt9_() {return mem_jt9.unlock();}
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; }
/** Constructor. Parses the command-line arguments, resets Rshare's * configuration (if requested), and sets up the GUI style and language * translation. */ Rshare::Rshare(QStringList args, int &argc, char **argv, const QString &dir) : QApplication(argc, argv) { mStartupTime = QDateTime::currentDateTime(); localServer = NULL; //Initialize connection to LocalServer to know if other process runs. { QString serverName = QString(TARGET); if (!args.isEmpty()) { // load into shared memory QBuffer buffer; buffer.open(QBuffer::ReadWrite); QDataStream out(&buffer); out << args; int size = buffer.size(); QSharedMemory newArgs; newArgs.setKey(serverName + "_newArgs"); if (newArgs.isAttached()) newArgs.detach(); if (!newArgs.create(size)) { std::cerr << "(EE) Rshare::Rshare Unable to create shared memory segment of size:" << size << " error:" << newArgs.errorString().toStdString() << "." << std::endl; #ifdef Q_OS_UNIX std::cerr << "Look with `ipcs -m` for nattch==0 segment. And remove it with `ipcrm -m 'shmid'`." << std::endl; //No need for windows, as it removes shared segment directly even when crash. #endif newArgs.detach(); ::exit(EXIT_FAILURE); } newArgs.lock(); char *to = (char*)newArgs.data(); const char *from = buffer.data().data(); memcpy(to, from, qMin(newArgs.size(), size)); newArgs.unlock(); // Connect to the Local Server of the main process to notify it // that a new process had been started QLocalSocket localSocket; localSocket.connectToServer(QString(TARGET)); std::cerr << "Rshare::Rshare waitForConnected to other instance." << std::endl; if( localSocket.waitForConnected(100) ) { std::cerr << "Rshare::Rshare Connection etablished. Waiting for disconnection." << std::endl; localSocket.waitForDisconnected(1000); newArgs.detach(); std::cerr << "Rshare::Rshare Arguments was sended." << std::endl << " To disable it, in Options - General - Misc," << std::endl << " uncheck \"Use Local Server to get new Arguments\"." << std::endl; ::exit(EXIT_SUCCESS); // Terminate the program using STDLib's exit function } newArgs.detach(); } // No main process exists // Or started without arguments // So we start a Local Server to listen for connections from new process localServer= new QLocalServer(); QObject::connect(localServer, SIGNAL(newConnection()), this, SLOT(slotConnectionEstablished())); updateLocalServer(); } #if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) qInstallMessageHandler(qt_msg_handler); #else qInstallMsgHandler(qt_msg_handler); #endif #ifndef __APPLE__ /* set default window icon */ setWindowIcon(QIcon(":/icons/logo_128.png")); #endif mBlink = true; QTimer *timer = new QTimer(this); timer->setInterval(500); connect(timer, SIGNAL(timeout()), this, SLOT(blinkTimer())); timer->start(); timer = new QTimer(this); timer->setInterval(60000); connect(timer, SIGNAL(timeout()), this, SIGNAL(minuteTick())); timer->start(); /* Read in all our command-line arguments. */ parseArguments(args); /* Check if we're supposed to reset our config before proceeding. */ if (_args.contains(ARG_RESET)) { Settings->reset(); } /* Handle the -loglevel and -logfile options. */ if (_args.contains(ARG_LOGFILE)) _log.open(_args.value(ARG_LOGFILE)); if (_args.contains(ARG_LOGLEVEL)) { _log.setLogLevel(Log::stringToLogLevel( _args.value(ARG_LOGLEVEL))); if (!_args.contains(ARG_LOGFILE)) _log.open(stdout); } if (!_args.contains(ARG_LOGLEVEL) && !_args.contains(ARG_LOGFILE)) _log.setLogLevel(Log::Off); /* config directory */ useConfigDir = false; if (dir != "") { setConfigDirectory(dir); } /** Initialize support for language translations. */ //LanguageSupport::initialize(); resetLanguageAndStyle(); /* Switch off auto shutdown */ setQuitOnLastWindowClosed ( false ); /* Initialize GxsIdDetails */ GxsIdDetails::initialize(); }