2023-04-11 11:13:23 -04:00
# include <QApplication>
2023-08-17 12:50:45 -04:00
# include <QMessageBox>
# include <QProcess>
# include <QPushButton>
# include <QFile>
2024-08-30 12:27:24 -04:00
# include <QLocale>
2023-04-11 11:13:23 -04:00
2024-04-05 22:47:58 -04:00
# include "FSUDAQ.h"
# include <QObject>
# include <QDebug>
2024-04-18 10:06:55 -04:00
# include <sys/resource.h>
2024-04-09 13:54:44 -04:00
// class CustomApplication : public QApplication{
// public:
// CustomApplication(int &argc, char **argv) : QApplication(argc, argv) {}
2024-04-05 22:47:58 -04:00
2024-04-09 13:54:44 -04:00
// protected:
// bool notify(QObject *receiver, QEvent *event) override{
// qDebug() << event->type() << "Receiver:" << receiver;
// return QApplication::notify(receiver, event);
// }
// };
2024-04-05 22:47:58 -04:00
2023-04-11 11:13:23 -04:00
int main ( int argc , char * argv [ ] ) {
2024-04-05 22:47:58 -04:00
2024-04-09 13:54:44 -04:00
// CustomApplication a(argc, argv);
QApplication a ( argc , argv ) ;
2023-04-11 11:13:23 -04:00
2024-08-30 12:27:24 -04:00
QLocale : : setDefault ( QLocale : : system ( ) ) ;
2024-04-18 10:06:55 -04:00
setpriority ( PRIO_PROCESS , 0 , - 20 ) ;
2023-08-17 12:50:45 -04:00
bool isLock = false ;
int pid = 0 ;
QFile lockFile ( DAQLockFile ) ;
if ( lockFile . open ( QIODevice : : Text | QIODevice : : ReadOnly ) ) {
QTextStream in ( & lockFile ) ;
QString line = in . readLine ( ) ;
isLock = line . toInt ( ) ;
lockFile . close ( ) ;
}
QFile pidFile ( PIDFile ) ;
if ( pidFile . open ( QIODevice : : Text | QIODevice : : ReadOnly ) ) {
QTextStream in ( & pidFile ) ;
QString line = in . readLine ( ) ;
pid = line . toInt ( ) ;
pidFile . close ( ) ;
}
if ( isLock ) {
qDebug ( ) < < " The DAQ program is already opened. PID is " + QString : : number ( pid ) + " , and delete the " + DAQLockFile ;
QMessageBox msgBox ;
msgBox . setWindowTitle ( " Oopss.... " ) ;
msgBox . setText ( " The DAQ program is already opened, or crashed perviously. \n PID is " + QString : : number ( pid ) + " \n You can kill the procee by \" kill -9 <pid> \" and delete the " + DAQLockFile + " \n or click the \" Kill \" button " ) ;
msgBox . setIcon ( QMessageBox : : Information ) ;
QPushButton * kill = msgBox . addButton ( " Kill and Open New " , QMessageBox : : AcceptRole ) ;
msgBox . setStandardButtons ( QMessageBox : : Ok ) ;
msgBox . setDefaultButton ( QMessageBox : : Ok ) ;
msgBox . exec ( ) ;
if ( msgBox . clickedButton ( ) = = kill ) {
remove ( DAQLockFile ) ;
QProcess : : execute ( " kill " , QStringList ( ) < < " -9 " < < QString : : number ( pid ) ) ;
} else {
return 0 ;
}
}
lockFile . open ( QIODevice : : Text | QIODevice : : WriteOnly ) ;
lockFile . write ( " 1 " ) ;
lockFile . close ( ) ;
pidFile . open ( QIODevice : : Text | QIODevice : : WriteOnly ) ;
pidFile . write ( QString : : number ( QCoreApplication : : applicationPid ( ) ) . toStdString ( ) . c_str ( ) ) ;
pidFile . close ( ) ;
2024-06-26 16:40:40 -04:00
FSUDAQ w ;
2023-04-11 11:13:23 -04:00
w . show ( ) ;
return a . exec ( ) ;
}