//
// Signal handler utility
//
// Copyright 2026, Jakob Østergaard Hegelund <joe@unthought.net>
// -----------------------------------------------------------------------
// This file is part of FanMan.
//
// FanMan is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FanMan 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FanMan. If not, see <https://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------
//

#ifndef _SIGH_HH
#define _SIGH_HH

#include <condition_variable>
#include <atomic>
#include <unordered_map>

class SigHandler {
public:
  SigHandler(int signal, std::condition_variable &cv);

  bool triggered() { return m_triggered; }
  void reset() { m_triggered = false; }

private:
  // CV to notify on trigger
  std::condition_variable &m_cv;
  // Whether we are triggered
  std::atomic<bool> m_triggered = false;

  // Our specific handler
  void handler();

  // Mapping from signal to SigHandler instance
  static std::unordered_map<int,SigHandler*> m_sigmap;
  // Our handler
  static void sighandler(int);
};

#endif

