/[svn]/qsampler/trunk/src/qsampler.cpp
ViewVC logotype

Diff of /qsampler/trunk/src/qsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3493 by capela, Sat Mar 9 21:47:46 2019 UTC revision 3496 by capela, Sun Mar 10 11:06:53 2019 UTC
# Line 52  Line 52 
52  // Singleton application instance stuff (Qt/X11 only atm.)  // Singleton application instance stuff (Qt/X11 only atm.)
53  //  //
54    
 #ifdef CONFIG_X11  
55  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
56    
 #include <unistd.h> /* for gethostname() */  
   
 #include <X11/Xatom.h>  
 #include <X11/Xlib.h>  
   
57  #define QSAMPLER_XUNIQUE "qsamplerApplication"  #define QSAMPLER_XUNIQUE "qsamplerApplication"
58    
59  #if QT_VERSION >= 0x050100  #if QT_VERSION < 0x050000
60    #ifdef CONFIG_X11
 #include <xcb/xcb.h>  
 #include <xcb/xproto.h>  
   
 #include <QAbstractNativeEventFilter>  
   
 class qsamplerXcbEventFilter : public QAbstractNativeEventFilter  
 {  
 public:  
   
         // Constructor.  
         qsamplerXcbEventFilter(qsamplerApplication *pApp)  
                 : QAbstractNativeEventFilter(), m_pApp(pApp) {}  
   
         // XCB event filter (virtual processor).  
         bool nativeEventFilter(const QByteArray& eventType, void *message, long *)  
         {  
                 if (eventType == "xcb_generic_event_t") {  
                         xcb_property_notify_event_t *pEv  
                                 = static_cast<xcb_property_notify_event_t *> (message);  
                         if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY  
                                 && pEv->state == XCB_PROPERTY_NEW_VALUE)  
                                 m_pApp->x11PropertyNotify(pEv->window);  
                 }  
                 return false;  
         }  
61    
62  private:  #include <unistd.h> /* for gethostname() */
63    
64          // Instance variable.  #include <X11/Xatom.h>
65          qsamplerApplication *m_pApp;  #include <X11/Xlib.h>
 };  
66    
67    #endif  // CONFIG_X11
68    #else
69    #include <QSharedMemory>
70    #include <QLocalServer>
71    #include <QLocalSocket>
72    #include <QHostInfo>
73  #endif  #endif
74    
75  #endif  // CONFIG_XUNIQUE  #endif  // CONFIG_XUNIQUE
 #endif  // CONFIG_X11  
76    
77    
78  // Constructor.  // Constructor.
# Line 160  qsamplerApplication::qsamplerApplication Line 133  qsamplerApplication::qsamplerApplication
133                          }                          }
134                  }                  }
135          }          }
 #ifdef CONFIG_X11  
136  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
137    #if QT_VERSION < 0x050000
138    #ifdef CONFIG_X11
139          m_pDisplay = NULL;          m_pDisplay = NULL;
140          m_aUnique = 0;          m_aUnique = 0;
141          m_wOwner = 0;          m_wOwner = 0;
142  #if QT_VERSION >= 0x050100  #endif  // CONFIG_X11
143          m_pXcbEventFilter = new qsamplerXcbEventFilter(this);  #else
144          installNativeEventFilter(m_pXcbEventFilter);          m_pMemory = NULL;
145            m_pServer = NULL;
146  #endif  #endif
147  #endif  // CONFIG_XUNIQUE  #endif  // CONFIG_XUNIQUE
 #endif  // CONFIG_X11  
148  }  }
149    
150    
151  // Destructor.  // Destructor.
152  qsamplerApplication::~qsamplerApplication (void)  qsamplerApplication::~qsamplerApplication (void)
153  {  {
 #ifdef CONFIG_X11  
154  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
155  #if QT_VERSION >= 0x050100  #if QT_VERSION >= 0x050000
156          removeNativeEventFilter(m_pXcbEventFilter);          if (m_pServer) {
157          delete m_pXcbEventFilter;                  m_pServer->close();
158                    delete m_pServer;
159                    m_pServer = NULL;
160            }
161            if (m_pMemory) {
162                    delete m_pMemory;
163                    m_pMemory = NULL;
164    }
165  #endif  #endif
166  #endif  // CONFIG_XUNIQUE  #endif  // CONFIG_XUNIQUE
 #endif  // CONFIG_X11  
167          if (m_pMyTranslator) delete m_pMyTranslator;          if (m_pMyTranslator) delete m_pMyTranslator;
168          if (m_pQtTranslator) delete m_pQtTranslator;          if (m_pQtTranslator) delete m_pQtTranslator;
169  }  }
# Line 194  qsamplerApplication::~qsamplerApplicatio Line 173  qsamplerApplication::~qsamplerApplicatio
173  void qsamplerApplication::setMainWidget ( QWidget *pWidget )  void qsamplerApplication::setMainWidget ( QWidget *pWidget )
174  {  {
175          m_pWidget = pWidget;          m_pWidget = pWidget;
 #ifdef CONFIG_X11  
176  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
177    #if QT_VERSION < 0x050000
178    #ifdef CONFIG_X11
179          m_wOwner = m_pWidget->winId();          m_wOwner = m_pWidget->winId();
180          if (m_pDisplay && m_wOwner) {          if (m_pDisplay && m_wOwner) {
181                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
182                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
183                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
184          }          }
 #endif  // CONFIG_XUNIQUE  
185  #endif  // CONFIG_X11  #endif  // CONFIG_X11
186    #endif
187    #endif  // CONFIG_XUNIQUE
188  }  }
189    
190    
# Line 211  void qsamplerApplication::setMainWidget Line 192  void qsamplerApplication::setMainWidget
192  // and raise its proper main widget...  // and raise its proper main widget...
193  bool qsamplerApplication::setup (void)  bool qsamplerApplication::setup (void)
194  {  {
 #ifdef CONFIG_X11  
195  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
196  #if QT_VERSION >= 0x050100  #if QT_VERSION < 0x050000
197          if (!QX11Info::isPlatformX11())  #ifdef CONFIG_X11
                 return false;  
 #endif  
198          m_pDisplay = QX11Info::display();          m_pDisplay = QX11Info::display();
199          if (m_pDisplay) {          if (m_pDisplay) {
200                  QString sUnique = QSAMPLER_XUNIQUE;                  QString sUnique = QSAMPLER_XUNIQUE;
# Line 266  bool qsamplerApplication::setup (void) Line 244  bool qsamplerApplication::setup (void)
244                          return true;                          return true;
245                  }                  }
246          }          }
 #endif  // CONFIG_XUNIQUE  
247  #endif  // CONFIG_X11  #endif  // CONFIG_X11
248          return false;          return false;
249    #else
250            m_sUnique = QCoreApplication::applicationName();
251            m_sUnique += '@';
252            m_sUnique += QHostInfo::localHostName();
253    #ifdef Q_OS_UNIX
254            m_pMemory = new QSharedMemory(m_sUnique);
255            m_pMemory->attach();
256            delete m_pMemory;
257    #endif
258            m_pMemory = new QSharedMemory(m_sUnique);
259            bool bServer = false;
260            const qint64 pid = QCoreApplication::applicationPid();
261            struct Data { qint64 pid; };
262            if (m_pMemory->create(sizeof(Data))) {
263                    m_pMemory->lock();
264                    Data *pData = static_cast<Data *> (m_pMemory->data());
265                    if (pData) {
266                            pData->pid = pid;
267                            bServer = true;
268                    }
269                    m_pMemory->unlock();
270            }
271            else
272            if (m_pMemory->attach()) {
273                    m_pMemory->lock(); // maybe not necessary?
274                    Data *pData = static_cast<Data *> (m_pMemory->data());
275                    if (pData)
276                            bServer = (pData->pid == pid);
277                    m_pMemory->unlock();
278            }
279            if (bServer) {
280                    QLocalServer::removeServer(m_sUnique);
281                    m_pServer = new QLocalServer();
282                    m_pServer->setSocketOptions(QLocalServer::UserAccessOption);
283                    m_pServer->listen(m_sUnique);
284                    QObject::connect(m_pServer,
285                            SIGNAL(newConnection()),
286                            SLOT(newConnectionSlot()));
287            } else {
288                    QLocalSocket socket;
289                    socket.connectToServer(m_sUnique);
290                    if (socket.state() == QLocalSocket::ConnectingState)
291                            socket.waitForConnected(200);
292                    if (socket.state() == QLocalSocket::ConnectedState) {
293                            socket.write(QCoreApplication::arguments().join(' ').toUtf8());
294                            socket.flush();
295                            socket.waitForBytesWritten(200);
296                    }
297            }
298            return !bServer;
299    #endif
300    #else
301            return false;
302    #endif  // !CONFIG_XUNIQUE
303  }  }
304    
305    
 #ifdef CONFIG_X11  
306  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
307    #if QT_VERSION < 0x050000
308    #ifdef CONFIG_X11
309    
310  void qsamplerApplication::x11PropertyNotify ( Window w )  void qsamplerApplication::x11PropertyNotify ( Window w )
311  {  {
312          if (m_pDisplay && m_pWidget && m_wOwner == w) {          if (m_pDisplay && m_pWidget && m_wOwner == w) {
# Line 310  void qsamplerApplication::x11PropertyNot Line 343  void qsamplerApplication::x11PropertyNot
343  }  }
344    
345    
 #if QT_VERSION < 0x050000  
346  bool qsamplerApplication::x11EventFilter ( XEvent *pEv )  bool qsamplerApplication::x11EventFilter ( XEvent *pEv )
347  {  {
348          if (pEv->type == PropertyNotify          if (pEv->type == PropertyNotify
# Line 318  bool qsamplerApplication::x11EventFilter Line 350  bool qsamplerApplication::x11EventFilter
350                  x11PropertyNotify(pEv->xproperty.window);                  x11PropertyNotify(pEv->xproperty.window);
351          return QApplication::x11EventFilter(pEv);          return QApplication::x11EventFilter(pEv);
352  }  }
 #endif  
353    
 #endif  // CONFIG_XUNIQUE  
354  #endif  // CONFIG_X11  #endif  // CONFIG_X11
355    #else
356    
357    // Local server conection slot.
358    void qsamplerApplication::newConnectionSlot (void)
359    {
360            QLocalSocket *pSocket = m_pServer->nextPendingConnection();
361            QObject::connect(pSocket,
362                    SIGNAL(readyRead()),
363                    SLOT(readyReadSlot()));
364    }
365    
366    // Local server data-ready slot.
367    void qsamplerApplication::readyReadSlot (void)
368    {
369            QLocalSocket *pSocket = qobject_cast<QLocalSocket *> (sender());
370            if (pSocket) {
371                    const qint64 nread = pSocket->bytesAvailable();
372                    if (nread > 0) {
373                            QByteArray data = pSocket->read(nread);
374                            // Just make it always shows up fine...
375                            m_pWidget->hide();
376                            m_pWidget->show();
377                            m_pWidget->raise();
378                            m_pWidget->activateWindow();
379                    }
380            }
381    }
382    
383    #endif
384    #endif  // CONFIG_XUNIQUE
385    
386    
387  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------

Legend:
Removed from v.3493  
changed lines
  Added in v.3496

  ViewVC Help
Powered by ViewVC