Skip to content

Commit

Permalink
Added a generic backend monitor for the event loop to monitor user-sp…
Browse files Browse the repository at this point in the history
…ecific events
  • Loading branch information
gansm committed Oct 5, 2023
1 parent 20bcb9c commit 6b7b65e
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 25 deletions.
2 changes: 2 additions & 0 deletions final/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ libfinal_la_SOURCES = \
dialog/fdialog.cpp \
dialog/ffiledialog.cpp \
dialog/fmessagebox.cpp \
eventloop/backend_monitor.cpp \
eventloop/eventloop.cpp \
eventloop/eventloop_functions.cpp \
eventloop/io_monitor.cpp \
Expand Down Expand Up @@ -115,6 +116,7 @@ finalcutdialoginclude_HEADERS = \
dialog/fmessagebox.h

finalcuteventloopinclude_HEADERS = \
eventloop/backend_monitor.h \
eventloop/eventloop.h \
eventloop/eventloop_functions.h \
eventloop/io_monitor.h \
Expand Down
2 changes: 2 additions & 0 deletions final/Makefile.clang
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ INCLUDE_HEADERS = \
dialog/fdialog.h \
dialog/ffiledialog.h \
dialog/fmessagebox.h \
eventloop/backend_monitor.h \
eventloop/eventloop.h \
eventloop/eventloop_functions.h \
eventloop/io_monitor.h \
Expand Down Expand Up @@ -107,6 +108,7 @@ OBJS = \
dialog/fdialog.o \
dialog/ffiledialog.o \
dialog/fmessagebox.o \
eventloop/backend_monitor.o \
eventloop/eventloop.o \
eventloop/eventloop_functions.o \
eventloop/io_monitor.o \
Expand Down
2 changes: 2 additions & 0 deletions final/Makefile.gcc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ INCLUDE_HEADERS = \
dialog/fdialog.h \
dialog/ffiledialog.h \
dialog/fmessagebox.h \
eventloop/backend_monitor.h \
eventloop/eventloop.h \
eventloop/eventloop_functions.h \
eventloop/io_monitor.h \
Expand Down Expand Up @@ -107,6 +108,7 @@ OBJS = \
dialog/fdialog.o \
dialog/ffiledialog.o \
dialog/fmessagebox.o \
eventloop/backend_monitor.o \
eventloop/eventloop.o \
eventloop/eventloop_functions.o \
eventloop/io_monitor.o \
Expand Down
98 changes: 98 additions & 0 deletions final/eventloop/backend_monitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/***********************************************************************
* backend_monitor.cpp - Monitoring general objects *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2023 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/

#include <unistd.h>

#include "final/eventloop/eventloop_functions.h"
#include "final/eventloop/eventloop.h"
#include "final/eventloop/backend_monitor.h"
#include "final/util/fsystem.h"

namespace finalcut
{

//----------------------------------------------------------------------
// class BackendMonitor
//----------------------------------------------------------------------

// constructors and destructor
//----------------------------------------------------------------------
BackendMonitor::BackendMonitor (EventLoop* eloop)
: Monitor(eloop)
{ }

//----------------------------------------------------------------------
BackendMonitor::~BackendMonitor() noexcept // destructor
{
// Close pipe file descriptors
static const auto& fsystem = FSystem::getInstance();
(void)fsystem->close(self_pipe.getReadFd());
(void)fsystem->close(self_pipe.getWriteFd());
}

// public methods of BackendMonitor
//----------------------------------------------------------------------
void BackendMonitor::setEvent() noexcept
{
// The event loop is notified by write access to the pipe
uint64_t buffer{1U};
auto successful = ::write ( self_pipe.getWriteFd()
, &buffer, sizeof(buffer) ) > 0;

if ( ! successful )
{
// Possible error handling
}
}

//----------------------------------------------------------------------
void BackendMonitor::clearEvent() noexcept
{
drainPipe(getFileDescriptor());
}

//----------------------------------------------------------------------
void BackendMonitor::trigger (short return_events)
{
clearEvent();
Monitor::trigger(return_events);
}


// private methods of BackendMonitor
//----------------------------------------------------------------------
void BackendMonitor::init()
{
static const auto& fsystem = FSystem::getInstance();
setEvents (POLLIN);

// Set up pipe for notification
if ( fsystem->pipe(self_pipe) != 0 )
{
throw monitor_error{"No pipe could be set up for the signal monitor."};
}

setFileDescriptor(self_pipe.getReadFd()); // Read end of pipe
setInitialized();
}

} // namespace finalcut
114 changes: 114 additions & 0 deletions final/eventloop/backend_monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/***********************************************************************
* backend_monitor.h - Monitoring general objects *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2023 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/

/* Inheritance diagram
* ═══════════════════
*
* ▕▔▔▔▔▔▔▔▔▔▏
* ▕ Monitor ▏
* ▕▁▁▁▁▁▁▁▁▁▏
* ▲
* │
* ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏1 1▕▔▔▔▔▔▔▔▔▔▔▏
* ▕ BackendMonitor ▏- - - - -▕ PipeData ▏
* ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▁▏
*/

#ifndef BACKEND_MONITOR_H
#define BACKEND_MONITOR_H

#include "final/eventloop/monitor.h"
#include "final/eventloop/pipedata.h"
#include "final/util/fstring.h"

namespace finalcut
{

//----------------------------------------------------------------------
// class BackendMonitor
//----------------------------------------------------------------------

class BackendMonitor final : public Monitor
{
public:
explicit BackendMonitor (EventLoop*);

// Disable default constructor
BackendMonitor() = delete;

// Disable copy constructor
BackendMonitor(const BackendMonitor&) = delete;

// Disable move constructor
BackendMonitor(const BackendMonitor&&) = delete;

// Destructor
~BackendMonitor() noexcept override;

// Accessor
auto getClassName() const -> FString override;

// Mutator
void setEvent() noexcept;

// Methods
template <typename T>
void init (handler_t, T&&);
void trigger (short) override;

private:
// Disable copy assignment operator (=)
auto operator = (const BackendMonitor&) -> BackendMonitor& = delete;

// Disable move assignment operator (=)
auto operator = (BackendMonitor&&) noexcept -> BackendMonitor& = delete;

// Mutator
void clearEvent() noexcept;

// Methods
void init();

// Data members
PipeData self_pipe{NO_FILE_DESCRIPTOR, NO_FILE_DESCRIPTOR};
};

// BackendMonitor inline functions
//----------------------------------------------------------------------
inline auto BackendMonitor::getClassName() const -> FString
{ return "BackendMonitor"; }

//----------------------------------------------------------------------
template <typename T>
inline void BackendMonitor::init (handler_t hdl, T&& uc)
{
if ( isInitialized() )
throw monitor_error{"This instance has already been initialised."};

setHandler (std::move(hdl));
setUserContext (std::forward<T>(uc));
init();
}

} // namespace finalcut

#endif // BACKEND_MONITOR_H
12 changes: 6 additions & 6 deletions final/eventloop/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class Monitor
// Destructor
virtual ~Monitor();

// Disable copy assignment operator (=)
auto operator = (const Monitor&) -> Monitor& = delete;

// Disable move assignment operator (=)
auto operator = (Monitor&&) noexcept -> Monitor& = delete;

// Accessors
virtual auto getClassName() const -> FString;
auto getEvents() const -> short;
Expand All @@ -104,12 +110,6 @@ class Monitor
// Inquiry
auto isActive() const -> bool;

// Disable copy assignment operator (=)
auto operator = (const Monitor&) -> Monitor& = delete;

// Disable move assignment operator (=)
auto operator = (Monitor&&) noexcept -> Monitor& = delete;

// Methods
virtual void resume();
virtual void suspend();
Expand Down
4 changes: 4 additions & 0 deletions final/eventloop/signal_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ SignalMonitor::~SignalMonitor() noexcept // destructor
static const auto& fsystem = FSystem::getInstance();
fsystem->sigaction (signal_number, getSigactionImpl()->getSigaction(), nullptr);

// Close pipe file descriptors
(void)fsystem->close(signal_pipe.getReadFd());
(void)fsystem->close(signal_pipe.getWriteFd());

// Remove monitor instance from the assignment table.
getSignalMonitorMap().erase(signal_number);
}
Expand Down
1 change: 0 additions & 1 deletion final/eventloop/signal_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#ifndef SIGNAL_MONITOR_H
#define SIGNAL_MONITOR_H

#include <array>
#include <map>
#include <memory>

Expand Down
31 changes: 30 additions & 1 deletion final/eventloop/timer_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@

#include <time.h>

#include <array>
#include <chrono>
#include <vector>

Expand Down Expand Up @@ -96,12 +95,27 @@ class TimerMonitorImpl : public Monitor
class PosixTimer : public TimerMonitorImpl
{
public:
// Using-declaration
using TimerMonitorImpl::TimerMonitorImpl;

// Constructor
explicit PosixTimer (EventLoop*);

// Disable copy constructor
PosixTimer (const PosixTimer&) = delete;

// Disable move constructor
PosixTimer (PosixTimer&&) noexcept = delete;

// Destructor
~PosixTimer() noexcept override;

// Disable copy assignment operator (=)
auto operator = (const PosixTimer&) -> PosixTimer& = delete;

// Disable move assignment operator (=)
auto operator = (PosixTimer&&) noexcept -> PosixTimer& = delete;

// Methods
template <typename T>
void init (handler_t, T&&);
Expand Down Expand Up @@ -141,12 +155,27 @@ inline void PosixTimer::init (handler_t hdl, T&& uc)
class KqueueTimer : public TimerMonitorImpl
{
public:
// Using-declaration
using TimerMonitorImpl::TimerMonitorImpl;

// Constructor
explicit KqueueTimer (EventLoop*);

// Disable copy constructor
KqueueTimer (const KqueueTimer&) = delete;

// Disable move constructor
KqueueTimer (KqueueTimer&&) noexcept = delete;

// Destructor
~KqueueTimer() noexcept override;

// Disable copy assignment operator (=)
auto operator = (const KqueueTimer&) -> KqueueTimer& = delete;

// Disable move assignment operator (=)
auto operator = (KqueueTimer&&) noexcept -> KqueueTimer& = delete;

// Methods
template <typename T>
void init (handler_t, T&&);
Expand Down
3 changes: 2 additions & 1 deletion final/final.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2014-2022 Markus Gans *
* Copyright 2014-2023 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -39,6 +39,7 @@
#include <final/dialog/fdialog.h>
#include <final/dialog/ffiledialog.h>
#include <final/dialog/fmessagebox.h>
#include <final/eventloop/backend_monitor.h>
#include <final/eventloop/eventloop.h>
#include <final/eventloop/io_monitor.h>
#include <final/eventloop/monitor.h>
Expand Down
4 changes: 2 additions & 2 deletions final/vterm/fvterm.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ inline auto FVTerm::FTermArea::contains (const FPoint& pos) const noexcept -> bo
const int x = pos.getX();
const int y = pos.getY();
return x >= offset_left
&& x <= offset_left + width + right_shadow - 1
&& x < offset_left + width + right_shadow
&& y >= offset_top
&& y <= offset_top + current_height - 1;
&& y < offset_top + current_height;
}

//----------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 6b7b65e

Please sign in to comment.