Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED][Linux]Multiple windows and event processing.  (Read 2577 times)

0 Members and 1 Guest are viewing this topic.

gsfare

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED][Linux]Multiple windows and event processing.
« on: March 24, 2015, 11:13:36 pm »
Hi All,

I'm experimenting with multiple windows. As per the guidelines I'm creating each window in it's own thread and handling the events within that thread context. However  the event queue appears to be generic between the windows. What this means is that the events being triggered in one window are sometimes handled in another window.

I have a small test app here that creates two windows. Sometimes when closing the second window the first window will process the event. It's not limited to that event but all events.

Not included in the source for simplicity I have added a call to XInitThreads() as there appears to be some thread safety issues surrounding the xcb implementation and suggestions indicate that it should be added.

I'm testing on a Linux system so wondering if I'm either doing something stupid (very open to that fact!) or there is a genuine problem in the underlying implementation. I tried forcing the thread creation to be synchronous, just in case there were issues around that, but it has not helped.

When mousing over only one of the windows I get this type of output,

Event from window 1
Event from window 2
Event from window 2
Event from window 2
Event from window 1
Event from window 2
Event from window 1
Event from window 1
Event from window 2
Event from window 2
Event from window 1
Event from window 2

I'm working from the git repo, last commit was

commit d53338298ab56c41a4bd5d065001dfcfe223deef
Author: Lukas Dürrenberger <eXpl0it3r@sfml-dev.org>
Date:   Wed Mar 11 08:46:40 2015 +0100

"uname -a" for OS

Linux 3.13.0-39-generic #66-Ubuntu SMP Tue Oct 28 13:30:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

#include <SFML/Window.hpp>

class Chart
{
   public:

      Chart( void );
      ~Chart( void );

      void wait( void );

   private:

      static void mainWindowThread( void *threadData );
      sf::Thread *mainWindowThread_;
      static int windowNumber_;
};

int Chart::windowNumber_ = 0;

Chart::Chart( void )
{
   mainWindowThread_ = new sf::Thread( mainWindowThread, this );
   mainWindowThread_->launch();
}

Chart::~Chart( void )
{
   if( mainWindowThread_ != NULL )
   {
      mainWindowThread_->wait();
      delete mainWindowThread_;
   }
}

void Chart::wait( void )
{
   mainWindowThread_->wait();
}

void Chart::mainWindowThread( void *threadData )
{
   char title[50];
   Chart *chart = (Chart *)threadData;
   sf::Event event;

   int num=++windowNumber_;
   snprintf( title, 50, "Window %d", windowNumber_ );
   sf::Window window(sf::VideoMode(800, 600), title);


   // run the program as long as the window is open
   while (window.isOpen())
   {
      // check all the window's events that were triggered since the last iteration of the loop
      while (window.waitEvent(event))
       {
           printf( "Event from window %d\n", num );
           // "close requested" event: we close the window
           if (event.type == sf::Event::Closed)
              window.close();
       }
   }
}

int main( int argc, const char *argv[] )
{
   Chart chart1;
   Chart chart2;

   chart1.wait();

   return( 0 );
}
 

« Last Edit: March 26, 2015, 11:16:09 am by gsfare »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: [Linux]Multiple windows and event processing.
« Reply #1 on: March 25, 2015, 02:25:33 pm »
Thanks for the bug report.

Running each window in its own thread isn't necessary. You just have to run the window in the thread you created it in. There is a slight difference ;).

I've opened up an issue on the tracker: https://github.com/SFML/SFML/issues/843

This will be fixed soon.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

gsfare

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [Linux]Multiple windows and event processing.
« Reply #2 on: March 25, 2015, 02:42:05 pm »
Thanks for the reply, no problem about the bug report, it looks like you understand what's going on there :) As it happens I did do some digging around and I was just posting when you replied. Maybe it will be of some use so I'll update just in case.

What I found out is that in the abstraction layer each window created by xcb is linked to a shared display by the following function in the file src/SFML/Window/Unix/Display.cpp

Display* OpenDisplay()

The variable used, sharedDisplay, is reference counted. As a quick test I just hacked the reference count so it was always zero, so a new display (and a new connection) is created for each window in the xcb abstraction. With this I do see the correct behaviour, i.e. each window is receiving it's own specific messages, but it'll obviously break a lot of other things!

I've subscribed to the bug report you mention and I'll keep an eye on it and contribute if I can.



binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

gsfare

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [Linux]Multiple windows and event processing.
« Reply #4 on: March 26, 2015, 11:15:37 am »
That was quick  :)

I just checked it out and I can confirm it is working for me as expected.

Thanks for your time! Keep up the good work.