/[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 2677 by capela, Thu Oct 9 08:50:14 2014 UTC revision 2839 by capela, Tue Aug 25 18:36:55 2015 UTC
# Line 1  Line 1 
1  // qsampler.cpp  // qsampler.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2004-2014, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2004-2015, rncbc aka Rui Nuno Capela. All rights reserved.
5     Copyright (C) 2007, 2008 Christian Schoenebeck     Copyright (C) 2007, 2008 Christian Schoenebeck
6    
7     This program is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or
# Line 53  Line 53 
53  // Singleton application instance stuff (Qt/X11 only atm.)  // Singleton application instance stuff (Qt/X11 only atm.)
54  //  //
55    
56    #if QT_VERSION < 0x050000
57  #if defined(Q_WS_X11)  #if defined(Q_WS_X11)
58    #define CONFIG_X11
59    #endif
60    #else
61    #if defined(QT_X11EXTRAS_LIB)
62    #define CONFIG_X11
63    #endif
64    #endif
65    
66    
67    #ifdef CONFIG_X11
68    #ifdef CONFIG_XUNIQUE
69    
70  #include <QX11Info>  #include <QX11Info>
71    
72  #include <X11/Xatom.h>  #include <X11/Xatom.h>
73  #include <X11/Xlib.h>  #include <X11/Xlib.h>
74    
75  #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"  #define QSAMPLER_XUNIQUE "qsamplerApplication"
76    
77    #if QT_VERSION >= 0x050100
78    
79    #include <xcb/xcb.h>
80    #include <xcb/xproto.h>
81    
82    #include <QAbstractNativeEventFilter>
83    
84    class qsamplerApplication;
85    
86    class qsamplerXcbEventFilter : public QAbstractNativeEventFilter
87    {
88    public:
89    
90            // Constructor.
91            qsamplerXcbEventFilter(qsamplerApplication *pApp)
92                    : QAbstractNativeEventFilter(), m_pApp(pApp) {}
93    
94            // XCB event filter (virtual processor).
95            bool nativeEventFilter(const QByteArray& eventType, void *message, long *);
96    
97    private:
98    
99            // Instance variable.
100            qsamplerApplication *m_pApp;
101    };
102    
103  #endif  #endif
104    
105    #endif  // CONFIG_XUNIQUE
106    #endif  // CONFIG_X11
107    
108    
109  class qsamplerApplication : public QApplication  class qsamplerApplication : public QApplication
110  {  {
111  public:  public:
112    
113          // Constructor.          // Constructor.
114          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
115                  m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)                      m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)
116          {          {
117                  // Load translation support.                  // Load translation support.
118                  QLocale loc;                  QLocale loc;
# Line 125  public: Line 167  public:
167                                  }                                  }
168                          }                          }
169                  }                  }
170          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
171            #ifdef CONFIG_XUNIQUE
172                    // Instance uniqueness initialization...
173                  m_pDisplay = QX11Info::display();                  m_pDisplay = QX11Info::display();
174                  m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);                  m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
175                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
176                  m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);                  m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
177                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
178            #if QT_VERSION >= 0x050100
179                    m_pXcbEventFilter = new qsamplerXcbEventFilter(this);
180                    installNativeEventFilter(m_pXcbEventFilter);
181          #endif          #endif
182            #endif  // CONFIG_XUNIQUE
183            #endif  // CONFIG_X11
184          }          }
185    
186          // Destructor.          // Destructor.
187          ~qsamplerApplication()          ~qsamplerApplication()
188          {          {
189            #ifdef CONFIG_X11
190            #ifdef CONFIG_XUNIQUE
191            #if QT_VERSION >= 0x050100
192                    removeNativeEventFilter(m_pXcbEventFilter);
193                    delete m_pXcbEventFilter;
194            #endif
195            #endif  // CONFIG_XUNIQUE
196            #endif  // CONFIG_X11
197                  if (m_pMyTranslator) delete m_pMyTranslator;                  if (m_pMyTranslator) delete m_pMyTranslator;
198                  if (m_pQtTranslator) delete m_pQtTranslator;                  if (m_pQtTranslator) delete m_pQtTranslator;
199          }          }
# Line 145  public: Line 202  public:
202          void setMainWidget(QWidget *pWidget)          void setMainWidget(QWidget *pWidget)
203          {          {
204                  m_pWidget = pWidget;                  m_pWidget = pWidget;
205          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
206            #ifdef CONFIG_XUNIQUE
207                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
208                  m_wOwner = m_pWidget->winId();                  m_wOwner = m_pWidget->winId();
209                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
210                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
211          #endif          #endif  // CONFIG_XUNIQUE
212            #endif  // CONFIG_X11
213          }          }
214    
215          QWidget *mainWidget() const { return m_pWidget; }          QWidget *mainWidget() const { return m_pWidget; }
# Line 159  public: Line 218  public:
218      // and raise its proper main widget...      // and raise its proper main widget...
219          bool setup()          bool setup()
220          {          {
221          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
222            #ifdef CONFIG_XUNIQUE
223                  if (m_wOwner != None) {                  if (m_wOwner != None) {
224                          // First, notify any freedesktop.org WM                          // First, notify any freedesktop.org WM
225                          // that we're about to show the main widget...                          // that we're about to show the main widget...
# Line 183  public: Line 243  public:
243                          XSync(m_pDisplay, false);                          XSync(m_pDisplay, false);
244                          XRaiseWindow(m_pDisplay, m_wOwner);                          XRaiseWindow(m_pDisplay, m_wOwner);
245                          // And then, let it get caught on destination                          // And then, let it get caught on destination
246                          // by QApplication::x11EventFilter...                          // by QApplication::native/x11EventFilter...
247                          QByteArray value = QSAMPLER_XUNIQUE;                          const QByteArray value = QSAMPLER_XUNIQUE;
248                          XChangeProperty(                          XChangeProperty(
249                                  m_pDisplay,                                  m_pDisplay,
250                                  m_wOwner,                                  m_wOwner,
# Line 196  public: Line 256  public:
256                          // Done.                          // Done.
257                          return true;                          return true;
258                  }                  }
259          #endif          #endif  // CONFIG_XUNIQUE
260            #endif  // CONFIG_X11
261                  return false;                  return false;
262          }          }
263    
264  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
265          bool x11EventFilter(XEvent *pEv)  #ifdef CONFIG_XUNIQUE
266            void x11PropertyNotify(Window w)
267          {          {
268                  if (m_pWidget && m_wOwner != None                  if (m_pWidget && m_wOwner == w) {
                         && pEv->type == PropertyNotify  
                         && pEv->xproperty.window == m_wOwner  
                         && pEv->xproperty.state == PropertyNewValue) {  
269                          // Always check whether our property-flag is still around...                          // Always check whether our property-flag is still around...
270                          Atom aType;                          Atom aType;
271                          int iFormat = 0;                          int iFormat = 0;
# Line 237  public: Line 296  public:
296                          if (iItems > 0 && pData)                          if (iItems > 0 && pData)
297                                  XFree(pData);                                  XFree(pData);
298                  }                  }
299            }
300    #if QT_VERSION < 0x050000
301            bool x11EventFilter(XEvent *pEv)
302            {
303                    if (pEv->type == PropertyNotify
304                            && pEv->xproperty.state == PropertyNewValue)
305                            x11PropertyNotify(pEv->xproperty.window);
306                  return QApplication::x11EventFilter(pEv);                  return QApplication::x11EventFilter(pEv);
307          }          }
308  #endif  #endif
309    #endif  // CONFIG_XUNIQUE
310    #endif  // CONFIG_X11
311                    
312  private:  private:
313    
# Line 250  private: Line 318  private:
318          // Instance variables.          // Instance variables.
319          QWidget *m_pWidget;          QWidget *m_pWidget;
320    
321  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
322    #ifdef CONFIG_XUNIQUE
323          Display *m_pDisplay;          Display *m_pDisplay;
324          Atom     m_aUnique;          Atom     m_aUnique;
325          Window   m_wOwner;          Window   m_wOwner;
326    #if QT_VERSION >= 0x050100
327            qsamplerXcbEventFilter *m_pXcbEventFilter;
328  #endif  #endif
329    #endif  // CONFIG_XUNIQUE
330    #endif  // CONFIG_X11
331  };  };
332    
333    
334    #ifdef CONFIG_X11
335    #ifdef CONFIG_XUNIQUE
336    #if QT_VERSION >= 0x050100
337    // XCB Event filter (virtual processor).
338    bool qsamplerXcbEventFilter::nativeEventFilter (
339            const QByteArray& eventType, void *message, long * )
340    {
341            if (eventType == "xcb_generic_event_t") {
342                    xcb_property_notify_event_t *pEv
343                            = static_cast<xcb_property_notify_event_t *> (message);
344                    if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY
345                            && pEv->state == XCB_PROPERTY_NEW_VALUE)
346                            m_pApp->x11PropertyNotify(pEv->window);
347            }
348            return false;
349    }
350    #endif
351    #endif  // CONFIG_XUNIQUE
352    #endif  // CONFIG_X11
353    
354    
355  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
356  // stacktrace - Signal crash handler.  // stacktrace - Signal crash handler.
357  //  //

Legend:
Removed from v.2677  
changed lines
  Added in v.2839

  ViewVC Help
Powered by ViewVC