Fixed event queue in FApplication

This commit is contained in:
Markus Gans 2020-05-21 14:53:51 +02:00
parent 5a60794b85
commit f20e036a9e
14 changed files with 68 additions and 48 deletions

View File

@ -1,3 +1,6 @@
2020-05-21 Markus Gans <guru.mail@muenster.de>
* Fixed the event queue in FApplication
2020-05-16 Markus Gans <guru.mail@muenster.de> 2020-05-16 Markus Gans <guru.mail@muenster.de>
* More direct access to the static FTerm functions * More direct access to the static FTerm functions

View File

@ -115,9 +115,6 @@ finalcut::FString EventDialog::getMouseButtonName (int btn_state)
case finalcut::fc::MiddleButton: case finalcut::fc::MiddleButton:
return "middle"; return "middle";
default:
return "unknown";
} }
return "unknown"; return "unknown";

View File

@ -100,6 +100,9 @@ FApplication::FApplication ( const int& _argc
FApplication::~FApplication() // destructor FApplication::~FApplication() // destructor
{ {
app_object = nullptr; app_object = nullptr;
if ( eventInQueue() )
event_queue.clear();
} }
@ -111,15 +114,15 @@ FApplication* FApplication::getApplicationObject()
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
std::shared_ptr<FLog>& FApplication::getLog() FApplication::FLogPtr& FApplication::getLog()
{ {
// Global logger object // Global logger object
static std::shared_ptr<FLog> logger = std::make_shared<FLogger>(); static FLogPtr logger(std::make_shared<FLogger>());
return logger; return logger;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FApplication::setLog (const std::shared_ptr<FLog>& logger) void FApplication::setLog (const FLogPtr& logger)
{ {
getLog() = logger; getLog() = logger;
} }
@ -183,25 +186,27 @@ void FApplication::quit()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FApplication::sendEvent (FObject* receiver, FEvent* event ) bool FApplication::sendEvent (FObject* receiver, FEvent* event )
{ {
if ( quit_now || app_exit_loop || ! receiver ) if ( quit_now || app_exit_loop || ! (bool(receiver) && bool(event)) )
return false; return false;
if ( ! isEventProcessable (receiver, event) ) if ( ! isEventProcessable (receiver, event) )
return false; return false;
// Sends the event event directly to receiver // Sends the event event directly to receiver
return receiver->event(event); bool ret = receiver->event(event);
event->send = true;
return ret;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FApplication::queueEvent (FObject* receiver, FEvent* event) void FApplication::queueEvent (FObject* receiver, FEvent* event)
{ {
if ( ! receiver ) if ( ! (bool(receiver) && bool(event)) )
return; return;
// queue this event // queue this event
eventPair send_event (receiver, std::make_shared<FEvent>(*event)); event->queued = true;
event_queue.push_back(send_event); event_queue.emplace_back (receiver, event);
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -209,8 +214,9 @@ void FApplication::sendQueuedEvents()
{ {
while ( eventInQueue() ) while ( eventInQueue() )
{ {
sendEvent( event_queue.front().first, const EventPair& event_pair = event_queue.front();
event_queue.front().second.get() ); event_pair.second->queued = false;
sendEvent(event_pair.first, event_pair.second);
event_queue.pop_front(); event_queue.pop_front();
} }
} }
@ -1170,7 +1176,7 @@ bool FApplication::isEventProcessable (const FObject* receiver, const FEvent* ev
&& ! window->getFlags().modal && ! window->getFlags().modal
&& ! window->isMenuWidget() ) && ! window->isMenuWidget() )
{ {
switch ( uInt(event->type()) ) switch ( uInt(event->getType()) )
{ {
case fc::KeyPress_Event: case fc::KeyPress_Event:
case fc::KeyUp_Event: case fc::KeyUp_Event:
@ -1194,8 +1200,8 @@ bool FApplication::isEventProcessable (const FObject* receiver, const FEvent* ev
} }
// Throw away mouse events for disabled widgets // Throw away mouse events for disabled widgets
if ( event->type() >= fc::MouseDown_Event if ( event->getType() >= fc::MouseDown_Event
&& event->type() <= fc::MouseMove_Event && event->getType() <= fc::MouseMove_Event
&& ! widget->isEnabled() ) && ! widget->isEnabled() )
return false; return false;

View File

@ -36,9 +36,17 @@ FEvent::FEvent (fc::events ev_type) // constructor
{ } { }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
fc::events FEvent::type() const fc::events FEvent::getType() const
{ return t; } { return t; }
//----------------------------------------------------------------------
bool FEvent::isQueued() const
{ return queued; }
//----------------------------------------------------------------------
bool FEvent::wasSent() const
{ return send; }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class FKeyEvent // class FKeyEvent
@ -193,13 +201,13 @@ FFocusEvent::~FFocusEvent() // destructor
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FFocusEvent::gotFocus() const bool FFocusEvent::gotFocus() const
{ {
return ( type() == fc::FocusIn_Event ); return ( getType() == fc::FocusIn_Event );
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FFocusEvent::lostFocus() const bool FFocusEvent::lostFocus() const
{ {
return ( type() == fc::FocusOut_Event ); return ( getType() == fc::FocusOut_Event );
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

@ -191,11 +191,11 @@ bool FObject::event (FEvent* ev)
{ {
// Receives events on this object // Receives events on this object
if ( ev->type() == fc::Timer_Event ) if ( ev->getType() == fc::Timer_Event )
{ {
onTimer ( static_cast<FTimerEvent*>(ev) ); onTimer ( static_cast<FTimerEvent*>(ev) );
} }
else if ( ev->type() == fc::User_Event ) else if ( ev->getType() == fc::User_Event )
{ {
onUserEvent ( static_cast<FUserEvent*>(ev) ); onUserEvent ( static_cast<FUserEvent*>(ev) );
} }

View File

@ -29,12 +29,14 @@
#include "final/ftermfreebsd.h" #include "final/ftermfreebsd.h"
#include "final/ftypes.h" #include "final/ftypes.h"
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(UNIT_TEST)
#define initCheck(ret_value) \ #define initCheck(ret_value) \
if ( ! isInitialized() ) \ if ( ! isInitialized() ) \
{ \ { \
warnNotInitialized(); \ warnNotInitialized(); \
return ret_value; \ return ret_value; \
} }
#endif
namespace finalcut namespace finalcut
{ {

View File

@ -26,12 +26,14 @@
#include "final/fterm.h" #include "final/fterm.h"
#include "final/ftermopenbsd.h" #include "final/ftermopenbsd.h"
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(UNIT_TEST)
#define initCheck(ret_value) \ #define initCheck(ret_value) \
if ( ! isInitialized() ) \ if ( ! isInitialized() ) \
{ \ { \
warnNotInitialized(); \ warnNotInitialized(); \
return ret_value; \ return ret_value; \
} }
#endif
namespace finalcut namespace finalcut
{ {

View File

@ -1386,7 +1386,7 @@ inline bool FVTerm::reallocateTextArea ( FTermArea* area
} }
catch (const std::bad_alloc&) catch (const std::bad_alloc&)
{ {
badAllocFunctionOutput ("FLineChanges[height] or FChar[size]"); badAllocOutput ("FLineChanges[height] or FChar[size]");
return false; return false;
} }
@ -1407,7 +1407,7 @@ inline bool FVTerm::reallocateTextArea (FTermArea* area, std::size_t size)
} }
catch (const std::bad_alloc&) catch (const std::bad_alloc&)
{ {
badAllocFunctionOutput ("FChar[size]"); badAllocOutput ("FChar[size]");
return false; return false;
} }
@ -1895,7 +1895,7 @@ const FChar FVTerm::getOverlappedCharacter (const FPoint& pos, FVTerm* obj)
// Gets the overlapped character for a given position // Gets the overlapped character for a given position
return getCharacter (overlapped_character, pos, obj); return getCharacter (overlapped_character, pos, obj);
} }
#include <unistd.h>
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FVTerm::init (bool disable_alt_screen) void FVTerm::init (bool disable_alt_screen)
{ {

View File

@ -1515,7 +1515,7 @@ bool FWidget::focusPrevChild()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FWidget::event (FEvent* ev) bool FWidget::event (FEvent* ev)
{ {
switch ( uInt(ev->type()) ) switch ( uInt(ev->getType()) )
{ {
case fc::KeyPress_Event: case fc::KeyPress_Event:
KeyPressEvent (static_cast<FKeyEvent*>(ev)); KeyPressEvent (static_cast<FKeyEvent*>(ev));

View File

@ -786,7 +786,7 @@ void FWindow::adjustSize()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FWindow::event (FEvent* ev) bool FWindow::event (FEvent* ev)
{ {
switch ( uInt(ev->type()) ) switch ( uInt(ev->getType()) )
{ {
case fc::WindowActive_Event: case fc::WindowActive_Event:
onWindowActive (ev); onWindowActive (ev);

View File

@ -90,6 +90,9 @@ class FObject;
class FApplication : public FWidget class FApplication : public FWidget
{ {
public: public:
// Typedef
typedef std::shared_ptr<FLog> FLogPtr;
// Constructor // Constructor
FApplication (const int&, char*[], bool = false); FApplication (const int&, char*[], bool = false);
@ -107,10 +110,10 @@ class FApplication : public FWidget
int getArgc() const; int getArgc() const;
char** getArgv() const; char** getArgv() const;
static FApplication* getApplicationObject(); static FApplication* getApplicationObject();
static std::shared_ptr<FLog>& getLog(); static FLogPtr& getLog();
// Mutator // Mutator
static void setLog (const std::shared_ptr<FLog>&); static void setLog (const FLogPtr&);
// Inquiry // Inquiry
static bool isQuit(); static bool isQuit();
@ -123,7 +126,7 @@ class FApplication : public FWidget
void quit(); void quit();
static bool sendEvent (FObject*, FEvent*); static bool sendEvent (FObject*, FEvent*);
void queueEvent (FObject*, FEvent*); void queueEvent (FObject*, FEvent*);
void sendQueuedEvents (); void sendQueuedEvents();
bool eventInQueue(); bool eventInQueue();
bool removeQueuedEvent (const FObject*); bool removeQueuedEvent (const FObject*);
static FWidget* processParameters (const int&, char*[]); static FWidget* processParameters (const int&, char*[]);
@ -139,8 +142,8 @@ class FApplication : public FWidget
private: private:
// Typedefs // Typedefs
typedef std::pair<FObject*, std::shared_ptr<FEvent> > eventPair; typedef std::pair<FObject*, FEvent*> EventPair;
typedef std::deque<eventPair> FEventQueue; typedef std::deque<EventPair> FEventQueue;
// Methods // Methods
void init (uInt64, uInt64); void init (uInt64, uInt64);

View File

@ -99,10 +99,18 @@ class FEvent // event base class
public: public:
FEvent() = default; FEvent() = default;
explicit FEvent(fc::events); explicit FEvent(fc::events);
fc::events type() const; fc::events getType() const;
bool isQueued() const;
bool wasSent() const;
private: private:
// Data members
fc::events t{fc::None_Event}; fc::events t{fc::None_Event};
bool queued{false};
bool send{false};
// Friend class
friend class FApplication;
}; };

View File

@ -184,7 +184,7 @@ void initScrollbar ( FScrollbarPtr& bar
} }
catch (const std::bad_alloc&) catch (const std::bad_alloc&)
{ {
badAllocFunctionOutput ("FScrollbar"); badAllocOutput ("FScrollbar");
return; return;
} }

View File

@ -37,23 +37,14 @@
#define null nullptr #define null nullptr
#define badAllocFunctionOutput(object_name) \
*FApplication::getLog() << FLog::Error \
<< "Not enough memory to alloc " \
<< (object_name) \
<< " in " \
<< __func__ << std::endl;
#define badAllocOutput(object_name) \ #define badAllocOutput(object_name) \
*FApplication::getLog() << FLog::Error \ *FApplication::getLog() << FLog::Error \
<< "Not enough memory to alloc " \ << __FILE__ << ":" << __LINE__ \
<< ": Not enough memory to alloc " \
<< (object_name) \ << (object_name) \
<< " in " \ << " in " \
<< getClassName() \
<< "::" \
<< __func__ << std::endl; << __func__ << std::endl;
//F_METHOD_CALLBACK
namespace namespace
{ {