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

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

  ViewVC Help
Powered by ViewVC