Results 1 to 5 of 5

Thread: trying to call Xlib inside qt app

  1. #1
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    1

    Default trying to call Xlib inside qt app

    Hi!

    Im trying to access Xlib directly to paint Xv overlays inside subwindows of a QT4 MDI app.
    Now Im getting some strange errors. I'm pretty sure I'm missing some basic concepts of XLib programming here...

    I started with a basic QT app and included code from this example:
    http://www.wedesoft.demon.co.uk/downloads/testxv2.cc

    I made an Xvideo class for all Xlib specific code. Before theres any data to display, I'd like to know if Xv is going to work. So I splitted the code in the example in "initialization part" and "code that can be used as base for the actual painting routine".

    Initialization is done in the constructor(which throws an exception on errors... some todos here)
    The rest is kept inside "setup()" for now.

    A stripped down version of my code looks like this:
    Qt Code:
    1. Xvideo::Xvideo()
    2. {
    3. m_nAdaptor = ~0; // equals -1
    4. m_pDisplay = NULL;
    5. m_pAdaptorInfo = NULL;
    6. m_nPort = 0;
    7. m_nColorkey = 0;
    8.  
    9. // --- start initialization ---
    10.  
    11. m_pDisplay = QX11Info::display () ;
    12. //alternatively: m_pDisplay = XOpenDisplay ( NULL );
    13.  
    14. // get version and release information about X video extension
    15. unsigned int nVersion, nRelease, nRequest_base, nEvent_base, nError_base;
    16. XvQueryExtension ( m_pDisplay, &nVersion, &nRelease, &nRequest_base,
    17. &nEvent_base, &nError_base );
    18.  
    19. // get video adaptor list for default display
    20. unsigned int nAdaptors;
    21. XvQueryAdaptors ( m_pDisplay, DefaultRootWindow ( m_pDisplay ), &nAdaptors,
    22. &m_pAdaptorInfo );
    23.  
    24. // search list for Xv-capable adaptor and try to grab a port
    25. for ( unsigned int i=0; i<nAdaptors; i++ )
    26. {
    27. // call XvGrabPort in some loop until its successfull...
    28. ...
    29. XvGrabPort ( m_pDisplay, p, CurrentTime )
    30. ...
    31. }
    32.  
    33. // get and set ColorKey data/config
    34. XvGetPortAttribute ( m_pDisplay, m_nPort,... );
    35. XvSetPortAttribute ( m_pDisplay, m_nPort, ...);
    36. }
    37.  
    38. bool Xvideo::setup ( Drawable window, int width, int height )
    39. {
    40. unsigned int formats;
    41. XvImageFormatValues *fo;
    42. // next call ends in segmentation fault
    43. fo = XvListImageFormats ( m_pDisplay, m_nPort, ( int * ) &formats );
    44. ...
    45. }
    To copy to clipboard, switch view to plain text mode 

    the constructor is called from the constructor of my "QT UI class":

    Qt Code:
    1. CamWindow() : QWidget()
    2. {
    3. setupUi ( this );
    4. Xvideo *m_pXvPort = NULL;
    5. try
    6. {
    7. m_pXvPort = new Xvideo();
    8. }
    9. catch ( QString strErr )
    10. {
    11. QMessageBox::warning(this, tr("x video extension"),
    12. strErr, QMessageBox::Ok);
    13. }
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    And setup is called directly after this window has been displayed (through a wrapper function in the "CamWindow" class:)

    Qt Code:
    1. CamWindow *w = new CamWindow();
    2. m_pWorkspace->addWindow(w);
    3. w->show();
    4. w->setup();
    To copy to clipboard, switch view to plain text mode 


    This may look a bit confusing, but the important points are:

    - Im doing some screen/adaptor specific calls in the constructor of a qwidget-derived class
    Also XvGrabPort is called here to grab a Port for future use.
    Returned data ( Display Pointer, AdaptorInfo Struct, Port handle, Colorkey ) is stored in member variables.

    - directly after the window is shown, I call XvListImageFormats with Display and Port read from member variables, which ends in a segmentation fault.

    - if i move XvListImageFormats to the constructor (to have it called in the same block as XvGrabPort), no errors occur.

    I dont use any extra threads yet. All XLib code is called from a signal function triggered by a QMenu action.

    Any idea what's goin wrong?
    Obviously my main problem is missing knowledge about Xlib programming and Qt internals. Are there any good references for platform dependent hacks in qt? Or 'modern' Xlib programming (besides man pages and 20 yo standards...)?

  2. #2
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    1

    Default Re: trying to call Xlib inside qt app

    I made further tests. I'm able to crash the program without any Xlib calls at all:

    I found out, that writing a simple member variable in (my class) Xvideo break things when its not inside the constructor ...?
    I commented out the whole constructor code of Xvideo. I placed
    >m_pDisplay = NULL;
    as first command in setup(), which is enough to let it crash.

    No I've even less ideas.... looks a bit like "we called the function of a deleted object"

    relevant code:

    class CamWindow :
    Qt Code:
    1. class CamWindow : public QWidget, private Ui::CamWindow
    2. {
    3. Q_OBJECT;
    4. protected:
    5. Xvideo *m_pXvPort;
    6. public:
    7. CamWindow() : QWidget()
    8. {
    9. setupUi ( this );
    10. Xvideo *m_pXvPort = NULL;
    11. try
    12. {
    13. m_pXvPort = new Xvideo();
    14. }
    15. catch ( QString strErr )
    16. {
    17. QMessageBox::warning ( this, tr ( "x video extension" ),
    18. strErr, QMessageBox::Ok );
    19. }
    20.  
    21. }
    22. ~CamWindow()
    23. {
    24. ;
    25. }
    26. void setup()
    27. {
    28. m_pXvPort->setup ( winId(), 100, 100 );
    29. }
    30. };
    To copy to clipboard, switch view to plain text mode 

    called functions of class Xvideo:
    Qt Code:
    1. Xvideo::Xvideo()
    2. {
    3.  
    4. }
    5.  
    6. bool Xvideo::setup ( Drawable window, int width, int height )
    7. {
    8. // next command crashes
    9. m_pDisplay = NULL;
    10. ....
    11.  
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    "main code":
    Qt Code:
    1. CamWindow *w = new CamWindow();
    2. m_pWorkspace->addWindow(w);
    3. w->show();
    4. w->setup();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: trying to call Xlib inside qt app

    What does gdb and/or valgrind have to say about it?
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: trying to call Xlib inside qt app

    Quote Originally Posted by phixx View Post
    class CamWindow : public QWidget, private Ui::CamWindow
    {
    Q_OBJECT;
    protected:
    Xvideo *m_pXvPort;
    public:
    CamWindow() : QWidget()
    {
    setupUi ( this );
    Xvideo *m_pXvPort = NULL;
    ...
    You have two m_pXvPort variables and you leave the member variable uninitialized.

  5. The following user says thank you to jacek for this useful post:

    phixx (12th February 2008)

  6. #5
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    1

    Default Re: trying to call Xlib inside qt app

    thanks a lot !!!
    now all my code runs through
    i dont see any results on the screen yet, but thats another story.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.